0

I have been plotting a histogram. My code looks like this:

x0=-15000
x1=15000
b=np.arange(x0,x1,(x1-x0)/250.)

plt.plot(b[0:-1], plt.hist(non_zeros(-e[0]), bins=b, normed=1, visible=0)[0], color = "k", label=r'$\gamma$ = 1.0')

I normed the histogram so the area under the curve is equal to 1. e[0] is just some data i take in from a document.

What i want now is to double check that the are under the histogram is equal to one. How can this be done?

Ffisegydd
  • 51,807
  • 15
  • 147
  • 125
David Halley
  • 417
  • 3
  • 6
  • 18
  • Can you add some code so that people trying to help you can actually run the code? Include your imports, define `non_zeros`, give an example value for `e[0]`. – Amit Kumar Gupta Oct 16 '14 at 08:16
  • Just a comment on your code, Python has proper True and False constants for booleans which are preferred above 1 and 0 (so you'd have `normed=True, visible=False` rather than what you have). – chthonicdaemon Oct 16 '14 at 08:18
  • Does this answer your question? [Integrate histogram in python?](https://stackoverflow.com/questions/29974976/integrate-histogram-in-python) – m81 Feb 20 '20 at 16:06

2 Answers2

6

You can calculate the area in this way:

import numpy
import matplotlib.pyplot as plt

x = numpy.random.randn(1000)

values, bins, _ = plt.hist(x, normed=True)
area = sum(numpy.diff(bins)*values)
chthonicdaemon
  • 19,180
  • 2
  • 52
  • 66
0
import numpy as np
import matplotlib.pyplot as plt

x0=-15000
x1=15000
b=np.arange(x0,x1,(x1-x0)/250.)
values, bins, patches = plt.hist(b, 20 ,range=[-15000,15000], facecolor='green',normed=0, alpha=0.5)

#simply here you need to have the lenght of your bins to have a probability for under the graph

len_bins= len(bins)-1
#here is the totaly area under your histogram, which is supposed to be 1 as you want = suming up all the values for of all bins
Total_Area= sum(values[0:len_bins])/sum(values)
#so now lets say you want to have half of the area
Half_Area= sum(values[0:len_bins/2])/sum(values)
Hany Zekry
  • 31
  • 2