Issue
I am trying to plot a histogram of a list using density=True option in the histogram method in matplotlib. The code is
import matplotlib.pyplot as plt
test=[1,2,3,1,1,3,2,1]
bins=100
plt.hist(test,bins,density=True)
plt.show()
I want a probability density plot. For the above example, the probability density of number 1 should be 0.5(4/8), of number 2 should be 0.25(2/8) and that of number 3 should be 0.25(2/8). But the code above does not give me the desired plot. The output with the above code is density for number 1 is 2.5, for numbers 2 and 3 are 1.25.
How can I get a probability density plot either using histogram in matplotlib or another plotting tool in python? Thanks.
Solution
According to the documentation of matplotlib.pyplot.hist
, if density=True
:
Each bin will display the bin's raw count divided by the total number of counts and the bin width...
Here, the bin width is 0.02 and not 1: (max(test) - min(test))/bins
You can also confirm this using print(plt.hist(test, bins, density=True)[1])
, which gives an array containing bin edges.
That's why, the probability density of 1 is 0.5/0.02 and not 0.5.
To get the desired result, you can try to get counts of unique values in the list and use plt.bar
:
import matplotlib.pyplot as plt
import numpy as np
test = [1,2,3,1,1,3,2,1]
values, counts = np.unique(test, return_counts=True)
plt.bar(values, counts/len(test))
plt.gca().set_xticks(values)
plt.gca().set_ylim(0, 1)
plt.show()
This gives:
Answered By - medium-dimensional
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.