如何使用 pyplot.bar 仅绘制正误差条?
Posted
技术标签:
【中文标题】如何使用 pyplot.bar 仅绘制正误差条?【英文标题】:How do I plot just the positive error bar with pyplot.bar? 【发布时间】:2012-10-30 01:16:30 【问题描述】:我正在尝试绘制 4 个带有正误差线的平均值和图中的最大值。
means = [26.82,26.4,61.17,61.55] # Mean Data
stds = [4.59,4.39,4.37,4.38] # Standard deviation Data
peakval = ['26.82','26.4','61.17','61.55'] # String array of means
ind = np.arange(len(means))
width = 0.35
colours = ['red','blue','green','yellow']
pyplot.figure()
pyplot.title('Average Age')
for i in range(len(means)):
pyplot.bar(ind[i],means[i],width,color=colours[i],align='center',yerr=stds[i],ecolor='k')
pyplot.ylabel('Age (years)')
pyplot.xticks(ind,('Young Male','Young Female','Elderly Male','Elderly Female'))
def autolabel(bars,peakval):
for ii,bar in enumerate(bars):
height = bars[ii]
pyplot.text(ind[ii], height-5, '%s'% (peakval[ii]), ha='center', va='bottom')
autolabel(means,peakval)
但是我不知道如何仅绘制正误差条。 所以我最终得到了这样的图表:
任何建议将不胜感激。
【问题讨论】:
【参考方案1】:如果我理解正确,您可以这样做:
import numpy as np
from matplotlib import pyplot
means = [26.82,26.4,61.17,61.55] # Mean Data
stds = [(0,0,0,0), [4.59,4.39,4.37,4.38]] # Standard deviation Data
peakval = ['26.82','26.4','61.17','61.55'] # String array of means
ind = np.arange(len(means))
width = 0.35
colours = ['red','blue','green','yellow']
pyplot.figure()
pyplot.title('Average Age')
pyplot.bar(ind, means, width, color=colours, align='center', yerr=stds, ecolor='k')
pyplot.ylabel('Age (years)')
pyplot.xticks(ind,('Young Male','Young Female','Elderly Male','Elderly Female'))
def autolabel(bars,peakval):
for ii,bar in enumerate(bars):
height = bars[ii]
pyplot.text(ind[ii], height-5, '%s'% (peakval[ii]), ha='center', va='bottom')
autolabel(means,peakval)
pyplot.show()
结果:
之所以有效,是因为您可以将2xN
列表作为yerr
传递,代表正负“偏移量”,请参阅documentation。
【讨论】:
以上是关于如何使用 pyplot.bar 仅绘制正误差条?的主要内容,如果未能解决你的问题,请参考以下文章
matplotlib可视化番外篇bar()--带误差棒的堆积柱状图