【发布时间】:2019-07-30 00:30:41
【问题描述】:
我正在尝试绘制带有不对称误差线的分组条形图。当误差线对称时,它会生成正确的图表。但是,对于非对称版本,误差线的长度是错误的。
这是一个最低限度可重现的代码:
# test with code from documentation
men_means, men_std = (20, 35, 30, 35, 27), (2, 3, 4, 1, 2)
women_means, women_std = (25, 32, 34, 20, 25), (3, 5, 2, 3, 3)
# dummy dataframe similar to what I will be using
avg = [20, 35, 30, 35, 27]
men_std_l = [19,33,28,34,25]
men_std_u = [22,37,31,39,29]
df = pd.DataFrame({'avg' :avg, 'low':men_std_l, 'high':men_std_u})
ind = np.arange(df.shape[0]) # the x locations for the groups
width = 0.35 # the width of the bars
fig, ax = plt.subplots()
rects1 = ax.bar(ind - width/2, df['avg'], width, yerr=[df['low'].values,df['high'].values], label='Men')
rects2 = ax.bar(ind + width/2, women_means, width, yerr=women_std,label='Women')
# Add some text for labels, title and custom x-axis tick labels, etc.
ax.set_ylabel('Scores')
ax.set_title('error bar is wrong for asymmetrical, correct otherwise')
ax.legend()
fig.tight_layout()
plt.show()
我已经尝试了Asymmetrical errorbar with pandas 的解决方案(获取 ValueError: In safezip, len(args[0])=5 but len(args1)=1) 和 plotting asymmetric errorbars using matplotlib(getting TypeError: Cannot cast array数据从 dtype('
非常感谢任何帮助。
【问题讨论】:
标签: matplotlib bar-chart errorbar