Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Color each errorbar with different color

I'm trying to plot an errorbar graph where each error bar may be either, say, red or green depending on whether the statistics used to compute the bar are significant.
I tried using an array of colors as an input to the c parameter, but that didn't work.

Does anyone know how to do that?

Here is the code that I have so far:

yerrs = np.array([quantiles[:,2],quantiles[:,3]])
print yerrs.shape
colors = ['r', 'b'] * (yerrs.shape[1]/2)
fig, axes = plt.subplots(nrows=2, sharex=True, sharey=True)
axes[0].errorbar(quantiles[:,0],quantiles[:,1], yerr=yerrs, c=colors)
axes[0].axhline(0, color='black')
axes[0].axvline(0, color='black')
axes[0].set_title('Fitted dist')

I then get the error:

ValueError: to_rgba: Invalid rgba arg "['r', 'b', 'r', 'b', 'r', 'b', 'r', 'b', 'r', 'b', 'r', 'b', 'r', 'b', 'r', 'b', 'r', 'b', 'r', 'b', 'r', 'b', 'r', 'b', 'r', 'b', 'r', 'b', 'r', 'b', 'r', 'b', 'r', 'b', 'r', 'b', 'r', 'b', 'r', 'b', 'r', 'b', 'r', 'b', 'r', 'b', 'r', 'b', 'r', 'b', 'r', 'b', 'r', 'b', 'r', 'b', 'r', 'b', 'r', 'b', 'r', 'b', 'r', 'b', 'r', 'b', 'r', 'b', 'r', 'b', 'r', 'b', 'r', 'b', 'r', 'b', 'r', 'b', 'r', 'b', 'r', 'b', 'r', 'b', 'r', 'b', 'r', 'b', 'r', 'b', 'r', 'b', 'r', 'b', 'r', 'b', 'r', 'b', 'r', 'b']"
could not convert string to float: r
like image 993
user1094206 Avatar asked May 27 '13 15:05

user1094206


People also ask

How do I make each color bar different in Matplotlib?

You can change the color of bars in a barplot using color argument. RGB is a way of making colors. You have to to provide an amount of red, green, blue, and the transparency value to the color argument and it returns a color.

Is it possible to have a separate Colour for each category in a bar graph Matplotlib?

Matplotlib PyPlot – Set Different Color(s) for Bars in Bar Plot. To set different colors for bars in a Bar Plot using Matplotlib PyPlot API, call matplotlib. pyplot. bar() function, and pass required color values, as list, to color parameter of bar() function.

How do I specify colors in Matplotlib?

The usual way to set the line color in matplotlib is to specify it in the plot command. This can either be done by a string after the data, e.g. "r-" for a red line, or by explicitely stating the color argument.


1 Answers

separate your data array into two groups and use "ecolor" to specify errorbar color.

axes[0].errorbar(x1, y1, yerr=yerr1, ecolor="r")
axes[0].errorbar(x2, y2, yerr=yerr2, ecolor="b")

How to separate data into portions 1 and 2 should be trivial to you, but let me know if you are uncertain.

like image 146
nye17 Avatar answered Sep 28 '22 02:09

nye17