I have created a histogram with matplotlib
using the pyplot.hist()
function. I would like to add a Poison error square root of bin height (sqrt(binheight)
) to the bars. How can I do this?
The return tuple of .hist()
includes return[2]
-> a list of 1 Patch objects. I could only find out that it is possible to add errors to bars created via pyplot.bar()
.
You need to use a bar chart type, instead of histogram type to get error bars. Then you put the error bars on each bar.
errorbar() method is used to create a line plot with error bars. The two positional arguments supplied to ax. errorbar() are the lists or arrays of x, y data points. The two keyword arguments xerr= and yerr= define the error bar lengths in the x and y directions.
Indeed you need to use bar. You can use to output of hist
and plot it as a bar:
import numpy as np
import pylab as plt
data = np.array(np.random.rand(1000))
y,binEdges = np.histogram(data,bins=10)
bincenters = 0.5*(binEdges[1:]+binEdges[:-1])
menStd = np.sqrt(y)
width = 0.05
plt.bar(bincenters, y, width=width, color='r', yerr=menStd)
plt.show()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With