【问题标题】:matplotlib asymmetric errorbar showing wrong informationmatplotlib 不对称误差条显示错误信息
【发布时间】: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


    【解决方案1】:

    回答我自己的问题,因为我无法从文档中理解错误的下限和上限是什么。事后看来,如果我不习惯在r 中使用ggplot,应该会更清楚。

    非对称误差条的 matplotlib 版本需要在条形高度上加减值。 它不希望用户提供上限和下限,而是应该添加和减去的数字。因此,我需要以下内容:

    xel = df['avg'].values - df['low'].values
    xeh = df['high'].values - df['avg'].values
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-08-06
      • 1970-01-01
      • 1970-01-01
      • 2021-02-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多