【发布时间】:2012-10-29 22:39:55
【问题描述】:
我需要绘制一个带有不对称误差线的条形图...
matplotlib.pyplot.bar 函数的文档说:
详细信息:xerr 和 yerr 直接传递给 errorbar(),因此它们也可以具有 2xN 的形状以实现独立 下限和上限错误的规范。
但是,我不能给 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 数组? 如果答案是肯定的...如何绘制带有不对称误差线的条形图?
感谢您的宝贵时间!
【问题讨论】:
标签: python matplotlib