49

How can the line width of the error bar caps in Matplotlib be changed?

I tried the following code:

(_, caplines, _) = matplotlib.pyplot.errorbar(
    data['distance'], data['energy'], yerr=data['energy sigma'],
    capsize=10, elinewidth=3)

for capline in caplines:
    capline.set_linewidth(10)
    capline.set_color('red')

pp.draw()

Unfortunately, this updates the color of the caps, but does not update the line width of the caps!

The resulting effect is similar to the "fat error bar lines / thin caps" in the following image: enter image description here

It would be nice to have "fat" bar caps, in the case; how can this be done, in Matplotlib? Drawing the bar caps "manually", one by one with plot() would work, but a simpler alternative would be best.

Trenton McKinney
  • 56,955
  • 33
  • 144
  • 158
Eric O. Lebigot
  • 91,433
  • 48
  • 218
  • 260

2 Answers2

41

EOL, you were very close..,

distance = [1,3,7,9]
energy = [10,20,30,40]
sigma = [1,3,2,5]

(_, caps, _) = plt.errorbar(distance, energy, sigma, capsize=20, elinewidth=3)

for cap in caps:
    cap.set_color('red')
    cap.set_markeredgewidth(10)

plt.show

enter image description here

set_markeredgewidth sets the width of the cap lines.

Matplotlib objects have so many attributes that often it is difficult to remember the right ones for a given object. IPython is a very useful tool for introspecting matplotlib. I used it to analyze the properties of the 2Dlines correponding to the error cap lines and I found that and other marker properties.

Cheers

joaquin
  • 82,968
  • 29
  • 138
  • 152
  • Thanks a lot! We used the same method, but I somehow missed `set_markeredgewidth`. :) So, the caps are actually markers, for Matplotlib, as opposed to 2D lines. It seems to me that the `capsize` argument of `errorbar()` is equivalent to the `cap.set_markersize()`, so the latter could be removed, no? – Eric O. Lebigot Oct 02 '11 at 16:08
  • While you were commenting I was also realizing that. I changed also the picture. – joaquin Oct 02 '11 at 16:11
  • @joaquin, how do you use iPython for "introspecting matplotlib." This sounds like a very useful skill. – Blink Oct 08 '13 at 14:26
  • 1
    @William IPython with the option --pylab, sets an environment with the conditions to plot interactively with matplotlib. At the same time you can at any `object.` to list its methods and check the docs of selected ones with `object.method?`. In this way you can check *in vivo* how changes in a given property affects your figure. – joaquin Oct 09 '13 at 06:47
  • i'm not seeing how to do this when plotting a bar graph with errorbars using `bar`. – abcd May 01 '16 at 20:01
32

This is based on @joaquin's answer, but a little more concise (if you just want plain error caps with no special styling):

distance = [1,3,7,9]
energy = [10,20,30,40]
sigma = [1,3,2,5]

plt.errorbar(distance, 
    energy, 
    sigma, 
    capsize=5, 
    elinewidth=2,
    markeredgewidth=2)

enter image description here

John
  • 1,335
  • 12
  • 17