Python – matplotlib bar with asymmetrical error bars

matplotlibpython

I need to plot a bar graph with asymmetrical error bars…

The documentation of the matplotlib.pyplot.bar function says:

Detail: xerr and yerr are passed directly to
errorbar(), so they can also have shape 2xN for independent
specification of lower and upper errors.

But, I can not give an 2xN array to the yerr…

import numpy as np
import matplotlib.pyplot as plt

plt.bar(xrange(5), [2,5,3,4,7], yerr=[[1,4,2,3,6],[4,10,6,8,14]]) #DO NOT work!

And show me the following error:

Traceback (most recent call last):
  File "bar_stacked.py", line 9, in <module>
    plt.bar(xrange(5), [2,5,3,4,7], yerr=[[1,4,2,3,6],[4,10,6,8,14]])
  File "/usr/lib/pymodules/python2.7/matplotlib/pyplot.py", line 1742, in bar
    ret = ax.bar(left, height, width, bottom, color, edgecolor, linewidth, yerr, xerr, ecolor, capsize, align, orientation, log, **kwargs)
  File "/usr/lib/pymodules/python2.7/matplotlib/axes.py", line 4253, in bar
    "incompatible sizes: bar() argument 'yerr' must be len(%s) or scalar" % nbars)
ValueError: incompatible sizes: bar() argument 'yerr' must be len(5) or scalar

But, instead this function:

import numpy as np
import matplotlib.pyplot as plt

plt.errorbar(xrange(5), [2,5,3,4,7], yerr=[[1,4,2,3,6],[4,10,6,8,14]])

Works fine.

Does the matplotlib.pyplot.bar no longer support the 2xN arrays for yerr?
If the answere is yes… How can I plot a bar graph with asymmetrical error bars?

Thanks for your time!

Best Answer

Which version of matplotlib are you using?

With the 1.1.1 (latest stable) version your code work flawlessly.

Related Topic