I have created an errorbar plot in matplotlib with the mean and confidence intervals of each bin. Now, I am trying to add a text label above each errorbar that states the number of observations in each 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().
Also when testing out what happens when printing get_xdata() it outputs the bins like so:
for line in bar1.lines:
print(line.get_xdata())
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. Any help greatly appreciated.
Update: Adding dataframe (df) code
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]})
CodePudding user response:
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. 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.

