# How do I add error bars on a histogram?

I've created a histogram to see the number of similar values in a list.

data = np.genfromtxt("Pendel-Messung.dat") stdm = (np.std(data))/((700)**(1/2)) breite = 700**(1/2) fig2 = plt.figure() ax1 = plt.subplot(111) ax1.set_ylim(0,150) ax1.hist(data, bins=breite) ax2 = ax1.twinx() ax2.set_ylim(0,150/700) plt.show()

I want to create error bars (the error being stdm) in the middle of each bar of the histogram. I know I can create errorbars using

plt.errorbar("something", data, yerr = stdm)

But how do I make them start in the middle of each bar? I thought of just adding breite/2, but that gives me an error.

Sorry, I'm a beginner! Thank you!

## Answers

ax.hist returns the bin edges and the frequencies (n) so we can use those for x and y in the call to errorbar. Also, the bins input to hist takes either an integer for the number of bins, or a sequence of bin edges. I think you we trying to give a bin width of breite? If so, this should work (you just need to select an appropriate xmax):

n,bin_edges,patches = ax.hist(data,bins=np.arange(0,xmax,breite)) x = bin_edges[:-1]+breite/2. ax.errorbar(x,n,yerr=stdm,linestyle='None')