Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting Different error bar colors in bar plot in matplotlib

Following Setting Different Bar color in matplotlib Python

I would like to change the error bar colors. I have figured out a way after a number of attempts:

a = plt.gca()
b = a.bar(range(4), [2]*4, yerr=range(4))
c = a.get_children()[8]
c.set_color(['r','r','b','r'])

Is there any better way? Certainly a.get_children()[8] is not a general solution at all.

enter image description here

like image 412
colinfang Avatar asked Feb 20 '14 15:02

colinfang


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.

How do you specify colors for different bars of a bar chart?

In a chart, click to select the data series for which you want to change the colors. On the Format tab, in the Current Selection group, click Format Selection. tab, expand Fill, and then do one of the following: To vary the colors of data markers in a single-series chart, select the Vary colors by point check box.

Which parameter is used to set the color of a error bar in bar plots?

ecolor: This parameter is an optional parameter. And it is the color of the errorbar lines with default value NONE. elinewidth: This parameter is also an optional parameter.


2 Answers

If you just want to set them to a single color, use the error_kw kwarg (expected to be a dict of keyword arguments that's passed on to ax.errorbar).

Also, just so you know, you can pass a sequence of facecolors directly to bar, though this won't change the errorbar color.

As a quick example:

import matplotlib.pyplot as plt  fig, ax = plt.subplots()  ax.bar(range(4), [2] * 4, yerr=range(1, 5), alpha=0.5,        color=['red', 'green', 'blue', 'cyan', 'magenta'],        error_kw=dict(ecolor='gray', lw=2, capsize=5, capthick=2)) ax.margins(0.05)  plt.show() 

enter image description here

However, if you want the errorbars to be different colors, you'll either need to plot them individually or modify them afterwards.

If you use the latter option, the capline colors actually can't be changed individually (note that they're not changed in @falsetru's example either). For example:

import matplotlib.pyplot as plt  fig, ax = plt.subplots() colors = ['red', 'green', 'blue', 'cyan', 'magenta']  container = ax.bar(range(4), [2] * 4, yerr=range(1, 5), alpha=0.5, color=colors,        error_kw=dict(lw=2, capsize=5, capthick=2)) ax.margins(0.05)  connector, caplines, (vertical_lines,) = container.errorbar.lines vertical_lines.set_color(colors)  plt.show() 

enter image description here

The caplines object in the answer above is a tuple of two Line2Ds: One line for all of the top caps, and one line for all of the bottom caps. There's not way to change the colors of the caps individually (it's easy to set them all to the same color) without removing that artist and creating a LineCollection in its place.

Therefore, you're better off just plotting the errorbars individually in this case.

E.g.

import matplotlib.pyplot as plt  x, height, error = range(4), [2] * 4, range(1,5) colors = ['red', 'green', 'blue', 'cyan', 'magenta']  fig, ax = plt.subplots() ax.bar(x, height, alpha=0.5, color=colors) ax.margins(0.05)  for pos, y, err, color in zip(x, height, error, colors):     ax.errorbar(pos + 0.4, y, err, lw=2, capsize=5, capthick=2, color=color)  plt.show() 

enter image description here

like image 129
Joe Kington Avatar answered Sep 21 '22 22:09

Joe Kington


Not a general solution neither, but here it is.

a = plt.gca()
b = a.bar(range(4), [2]*4, yerr=range(4))
c = b.errorbar.lines[2][b.errorbar.has_xerr] # <----
c.set_color(['r', 'r', 'b', 'r'])


# from matplotlib.collections import LineCollection
# next(i
#      for i, x in enumerate(b.errorbar.lines)
#      if x and any(isinstance(y, LineCollection) for y in x)) == 2
like image 20
falsetru Avatar answered Sep 19 '22 22:09

falsetru