The Python Oracle

Plot a histogram such that the total height equals 1

This video explains
Plot a histogram such that the total height equals 1

--

Become part of the top 3% of the developers by applying to Toptal
https://topt.al/25cXVn

--

Music by Eric Matyas
https://www.soundimage.org
Track title: Popsicle Puzzles

--

Chapters
00:00 Question
02:18 Accepted answer (Score 38)
03:23 Answer 2 (Score 14)
03:38 Answer 3 (Score 5)
04:06 Answer 4 (Score 4)
04:47 Thank you

--

Full question
https://stackoverflow.com/questions/2224...

Question links:
[answer]: https://stackoverflow.com/a/16399202/775...
[Carsten König]: https://stackoverflow.com/users/1542814/...
[plotting histograms whose bar heights sum to 1 in matplotlib]: https://stackoverflow.com/questions/3866...

Answer 1 links:
[here]: https://stackoverflow.com/a/16399202/154...

--

Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...

--

Tags
#python #matplotlib #seaborn #histogram #densityplot

#avk47



ACCEPTED ANSWER

Score 46


When plotting a normalized histogram, the area under the curve should sum to 1, not the height.

In [44]:

import matplotlib.pyplot as plt
k=(3,3,3,3)
x, bins, p=plt.hist(k, density=True)  # used to be normed=True in older versions
from numpy import *
plt.xticks( arange(10) ) # 10 ticks on x axis
plt.show()  
In [45]:

print bins
[ 2.5  2.6  2.7  2.8  2.9  3.   3.1  3.2  3.3  3.4  3.5]

Here, this example, the bin width is 0.1, the area underneath the curve sums up to one (0.1*10).

x stores the height for each bins. p stores each of those individual bins objects (actually, they are patches. So we just sum up x and modify the height of each bin object.

To have the sum of height to be 1, add the following before plt.show():

for item in p:
    item.set_height(item.get_height()/sum(x))

enter image description here




ANSWER 2

Score 19


You could use the solution outlined here:

weights = np.ones_like(myarray)/float(len(myarray))
plt.hist(myarray, weights=weights)



ANSWER 3

Score 5


One way is to get the probabilities on your own, and then plot with plt.bar:

In [91]: from collections import Counter
    ...: c=Counter(k)
    ...: print c
Counter({1: 2, 3: 1, 4: 1})

In [92]: plt.bar(c.keys(), c.values())
    ...: plt.show()

result: enter image description here




ANSWER 4

Score 4


A normed histogram is defined such that the sum of products of width and height of each column is equal to the total count. That's why you are not getting your max equal to one.

However, if you still want to force it to be 1, you could use numpy and matplotlib.pyplot.bar in the following way

sample = np.random.normal(0,10,100)
#generate bins boundaries and heights
bin_height,bin_boundary = np.histogram(sample,bins=10)
#define width of each column
width = bin_boundary[1]-bin_boundary[0]
#standardize each column by dividing with the maximum height
bin_height = bin_height/float(max(bin_height))
#plot
plt.bar(bin_boundary[:-1],bin_height,width = width)
plt.show()