Is there a simple command in matplotlib that let's me take the integral of histogram over a certain range? If I plot a histogram with: fig = plt.hist(x, bins) Then, is there a command like fig.integral(bin1, bin2)? That will return the integral of the histogram from bin1 to bin2?
-
You really should provide a minimal example so we know what your variables are. Specifically, what is `bins`? If it's an integer, then you can use my answer but if not you need something more flexible like what cphlewis posted – Matthew Turner Apr 30 '15 at 18:51
-
`hist` now has a `cumulative` keyword to plot the cumulative. Not sure if you wanted the values or just the plot. – esmit Jan 31 '19 at 23:33
2 Answers
First, remember that the integral is just the total area underneath the curve. In the case of a histogram, the integral (in pseudo-python) is sum([bin_width[i] * bin_height[i] for i in bin_indexes_to_integrate])
.
As a reference, see this example of using a histogram in matplotlib: http://matplotlib.org/1.2.1/examples/pylab_examples/histogram_demo.html.
Here they separate the output of the plt.histogram
into three parts, n
, bins
, and patches
. We can use this separation to implement the "integral" you request like so.
Assuming bin1
and bin2
are indexes of the bins you want to integrate, then calculate the integral like so:
# create some dummy data to make a histogram of
import numpy as np
x = np.random.randn(1000)
nbins = 10
# use _ to assign the patches to a dummy variable since we don't need them
n, bins, _ = plt.hist(x, nbins)
# get the width of each bin
bin_width = bins[1] - bins[0]
# sum over number in each bin and mult by bin width, which can be factored out
integral = bin_width * sum(n[bin1:bin2])
If you've defined bins
to be a list with multiple widths, you have to do something like what @cphlewis said (this works w/ no off by one):
integral = sum(np.diff(bins[bin1:bin2])*n[bin1:bin2])
It's also worth taking a look at the API documentation for matplotlib.pyplot.hist.

- 3,564
- 2
- 20
- 21
-
I think this only works for adjacent bins? (esp. as they needn't all be of the same width?) – cphlewis Apr 30 '15 at 18:43
-
OP says they want to take integral from/to bin1 and bin2. I'd assumed `bins` was an integer, but you're right if it's a list with different widths this wouldn't work. – Matthew Turner Apr 30 '15 at 18:50
-
It wasn't clear to me that bin1 and bin2 are adjacent, like `bins[1]` and bins[2]` would be, or just argument names 1 and 2. @Jess, be careful if integrating over a distance or over irregular bins. – cphlewis Apr 30 '15 at 18:54
-
I had to slightly change the formula to avoid an off-by-one error. This worked for me: `integral = sum(np.diff(bins[bin1:bin2+1])*n[bin1:bin2])` – m81 Feb 20 '20 at 16:18
The plt.hist
command returns all the data you need to make one. If out = plt.hist(...)
, the bin heights are in out[0]
and the bin widths are diff(out[1])
. E.g.,
sum(out[0][4:7]*diff(out[1][4:8]))
for the integral over bins 4-6 inclusive. diff
calculates each bin-width, so it handles bins of different widths, and the multiplication happens element-wise, so calculates the areas of each rectangle in the histogram.

- 15,759
- 4
- 46
- 55