如何使用pyplot.bar绘制正误差线? [英] How do I plot just the positive error bar with pyplot.bar?

查看:141
本文介绍了如何使用pyplot.bar绘制正误差线?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试绘制4个带有正误差线的平均值以及该图中的最大值.

I'm trying to plot 4 average values with positive error bars and the max value within the plot.

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)    

但是我无法找出如何仅绘制正误差线. 所以我最终得到了一个像这样的图:

However I can can't find out how to plot only the positive error bars. So I end up with a graph like this:

任何建议将不胜感激.

推荐答案

如果我正确理解,则可以执行以下操作:

If I understood correctly you can do this:

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传递,代表正负偏移",请参见

It works because you can pass as yerr a 2xN list, representing the positive and negative "offsets", see the documentation.

这篇关于如何使用pyplot.bar绘制正误差线?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆