带有不对称误差条的matplotlib条

2024-06-28 11:22:08 发布

您现在位置:Python中文网/ 问答频道 /正文

我需要绘制一个带有不对称误差条的条形图。。。

matplotlib.pyplot.bar函数的文档显示:

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

但是,我不能给yerr一个2xN数组。。。

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!

并显示以下错误:

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

但是,相反,这个函数:

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]])

工作正常。

matplotlib.pyplot.bar是否不再支持yerr的2xN数组? 如果答案是肯定的。。。如何绘制带有不对称误差条的条形图?

谢谢你的时间!


Tags: inpyimportmatplotlibasline绘制bar