Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Matplotlib histogram with errorbars

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().

like image 575
bioslime Avatar asked Aug 02 '12 09:08

bioslime


People also ask

Can you have error bars on a histogram?

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.

How do you plot a graph with error bars in Python?

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.


1 Answers

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()

enter image description here

like image 163
imsc Avatar answered Oct 11 '22 05:10

imsc