Louis Thibault
Louis Thibault

Reputation: 21410

How can I normalize a histogram such that the sum of the heights is equal to 1?

I generated the figure below using the a call to matplotlib.pyplot.hist in which I passed the kwarg normed=True:

enter image description here

Upon further research, I realized that this kind of normalization works in such a way that the integral of the histogram is equal to 1. How can I plot this same data such that the sum of the heights of the bars equals 1?

In other words, I want each bit to represent the proportion of the whole that its values contain.

Upvotes: 3

Views: 3671

Answers (1)

Lev Levitsky
Lev Levitsky

Reputation: 65791

I'm not sure if there's a straightforward way, but you can manually divide all bar heights by the length of the input (the following is made in ipython --pylab to skip the imports):

inp = normal(size=1000)
h = hist(inp)

Which gives you

enter image description here

Now, you can do:

bar(h[1][:-1], h[0]/float(len(inp)), diff(h[1]))

and get

enter image description here

Upvotes: 3

Related Questions