简体   繁体   English

如何在平均值和置信区间误差条 python 之上添加计数文本?

[英]How to add text of count above mean and confidence interval errorbar python?

I have created an errorbar plot in matplotlib with the mean and confidence intervals of each bin.我在 matplotlib 中创建了一个误差条 plot,其中包含每个 bin 的平均值和置信区间。 Now, I am trying to add a text label above each errorbar that states the number of observations in each bin.现在,我正在尝试在每个错误栏上方添加一个文本 label,说明每个 bin 中的观察数。 So far I have:到目前为止,我有:

   binned   bin_count  val_mean  val_ci 
(0.1, 0.3]    10        3.13    14.20   
(0.3, 0.6]    40        -.1     12.98   
(0.6, 0.9]    31        1.8     12.59   
(0.9, 1.2]    4         .42.    1.42

bar1 = plt.errorbar(x = df.binned, y = df.val_mean,
             yerr = df.val_ci, marker = 'o', linestyle = '', capsize = 4)

i = 0
for line in bar1.lines:
    plt.text(x = line.get_xdata(), y = line.get_ydata()+ .1, s = str(df['bin_count'][i]))
    i += 1
plt.show()

But I keep getting the errors:但我不断收到错误:

AttributeError: 'tuple' object has no attribute 'get_xdata'

and

TypeError: only size-1 arrays can be converted to Python scalars

I have been looking at documentation on how to access the x and y values of Line2D object and what I have says get_xdata().我一直在查看有关如何访问 Line2D object 的 x 和 y 值以及我所说的 get_xdata() 的文档。

Also when testing out what happens when printing get_xdata() it outputs the bins like so:此外,在测试打印 get_xdata() 时会发生什么时,它会像这样输出 bin:

for line in bar1.lines: 
     print(line.get_xdata())

Output: Output:

 ['(.1, .3]', '(0.3, 0.6]', '(0.6, 0.9]', '(0.9, 1.2]']
 AttributeError: 'tuple' object has no attribute 'get_xdata'

I'm not sure what to do to correctly access the coordinates of each bar and print the bin_count text on top of the error bars.我不确定如何正确访问每个条的坐标并在误差条顶部打印 bin_count 文本。 Any help greatly appreciated.非常感谢任何帮助。

Update: Adding dataframe (df) code更新:添加 dataframe (df) 代码

df = pd.DataFrame({'binned': ['(0.1, 0.3]', '(0.3, 0.6]', '(0.6, 0.9]', '(0.9, 1.2]'],
                   'bin_count': [10, 40, 31, 4],
                   'val_mean': [3.13, -.1, 1.8, .42],
                   'val_ci': [14.20, 12.98, 12.59, 1.42]})  

Is this something you are trying to achieve?这是你想要达到的目标吗?

import matplotlib.pyplot as plt
import numpy

df = numpy.array([[
    0.1, 0.3, 10, 3.13, 14.2], [
    0.3, 0.6, 40, -0.1, 12.98], [
    0.6, 0.9, 31, 1.8, 12.59], [
    0.9, 1.2, 4, 0.42, 1.42]])

bar1 = plt.errorbar(df[:, 0], y=df[:, 3], yerr=df[:, 4], marker='o', linestyle='', capsize=4)


xx = bar1.lines[0].get_xdata()
yy = bar1.lines[0].get_ydata()
for i in range(len(xx)):
    plt.text(x=xx[i], y=yy[i] + .1, s=str(df[:, 2][i]))
    i += 1
plt.show()

You can make the necessary changes on your df and it should still work in my opinion.您可以对您的 df 进行必要的更改,我认为它应该仍然有效。 The end result I achieved with this is shown here.我用这个实现的最终结果显示在这里。

在此处输入图像描述

Now note that, in your case I assume your first column as an interval, but you can notice in your print statement that your interval is a string.现在请注意,在您的情况下,我假设您的第一列是一个间隔,但您可以在打印语句中注意到您的间隔是一个字符串。 I haven't invested any time in solving your interval problem as this question is intended for having bin counts as text.我没有投入任何时间来解决您的间隔问题,因为这个问题旨在将 bin 计数为文本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM