python - 使用 pyplot 打印错误栏上限上方的文本

标签 python matplotlib

我目前正在制作一个比较简单的条形图,但我想在相应条形上方添加平均值和标准差文本。问题是我的图表中的条形尺寸并不相同,因此我无法使用矩形对象来定义高度。这样做会产生如下图像: enter image description here

当然,这是完全 Not Acceptable ,所以我一直在尝试找到一种方法来完成同样的事情,除了误差线的上限。

我当前的代码是这样的:

bar = plt.bar(names, means, yerr=sterr, capsize=5, color=colors)
plt.yticks(np.arange(0,0.35,step=0.05))
plt.ylabel('Percentage of appearance')
plt.xlabel('Pot in question')   

# Add numbers in plot
for i, rectangle in enumerate(bar):
    height = rectangle.get_height()
    plt.text(rectangle.get_x() + rectangle.get_width()/2, height+0.01,
             '$\mu=$%s \n $\sigma=$%s' % (str(round(height,3))[1:], str(round(sterr[i],3))[1:]),
             ha='center', va='bottom')

plt.show()

新代码将用误差条图的大写字母替换 for 循环中的矩形。

继续我的尝试,我打开了 BarContainer 对象并发现了这个:

{'patches': [<matplotlib.patches.Rectangle object at 0x0000020C44BB8CF8>, <matplotlib.patches.Rectangle object at 0x0000020C44BB8FD0>, <matplotlib.patches.Rectangle object at 0x0000020C44BC6390>, <matplotlib.patches.Rectangle object at 0x0000020C44BC66D8>, <matplotlib.patches.Rectangle object at 0x0000020C44BC6A20>, <matplotlib.patches.Rectangle object at 0x0000020C44BC6DA0>, <matplotlib.patches.Rectangle object at 0x0000020C44BD4160>, <matplotlib.patches.Rectangle object at 0x0000020C44BD44E0>], 'errorbar': <ErrorbarContainer object of 3 artists>, 'eventson': False, '_oid': 0, '_propobservers': {}, '_remove_method': <function _AxesBase.add_container.<locals>.<lambda> at 0x0000020C3F71EF28>, '_label': '_container1'}

它有一个 ErrorbarContainer 对象,所以我尝试打开它,结果显示:

{'lines': (None, (<matplotlib.lines.Line2D object at 0x0000020C44BD4DA0>, <matplotlib.lines.Line2D object at 0x0000020C44BD4EB8>), (<matplotlib.collections.LineCollection object at 0x0000020C44BD4860>,)), 'has_xerr': False, 'has_yerr': True, 'eventson': False, '_oid': 0, '_propobservers': {}, '_remove_method': None, '_label': '_nolegend_'}

但是这些都没有给我任何关于如何继续的想法。

最佳答案

按误差线的高度缩放文本位置?

names = ['bob', 'sharon', 'han solo', 'the dude']
means = [14, 13, 1, 22]
sterr = [1, 3, 5, 1.4]
colors = ['red','green','blue','pink']

bar = plt.bar(names, means, yerr=sterr, capsize=5, color=colors)
plt.yticks(np.arange(0,0.35,step=0.05))
plt.ylabel('Percentage of appearance')
plt.xlabel('Pot in question')   

# Add numbers in plot
for i, rectangle in enumerate(bar):
    height = rectangle.get_height()
    plt.text(rectangle.get_x() + rectangle.get_width()/2, height+sterr[i] + 0.3,
         '$\mu=$%s \n $\sigma=$%s' % (str(round(height,3))[1:],     str(round(sterr[i],3))[1:]),
             ha='center', va='bottom')

plt.show()

enter image description here

虽然很粗糙,但你明白了。我认为,该图显示了您问题的解决方案,并使用虚构数据生成了更多解决方案。

关于python - 使用 pyplot 打印错误栏上限上方的文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51643995/

相关文章:

python - 删除python中的特定字符串和特定空行

python - python 中 elif 后面是否必须跟着 else 作为通用规则?

python - 解释 WAV 数据

python - Seaborn:Violinplot 遇到变量太多的困难?

python - Numpy:将矩阵与 3d 张量相乘——建议

python - 打印一个列表,三个点出现在子列表中

python - Jupyter 笔记本不再在外部窗口中打开 matplotlib 图?

python 3 : Installing gi package with pip

python - 带有方程式和 R2 文本的 Seaborn lmplot

matplotlib - 为 fiddle 图中的每个 fiddle 设置颜色