Reputation: 135
I get an incorrect pdf plot on following histogram reading.
Following is my source code for the above picture.
import numpy as np
import matplotlib.pyplot as plt
import statistics as st
import matplotlib.mlab as mlab
y1=[-54, -34, -45, -49, -51, -47, -52, -47, -51, -43, -43, -47, -45, -43, -42, -42, -43, -42, -41, -40, -45, -45, -47, -47, -47, -42, -40, -41, -46, -46, -42, -42, -42, -46, -41, -41, -41, -48, -44, -49, -45, -48, -45, -45, -44, -48, -51, -43, -45, -48, -51, -44, -50, -37, -39, -45, -45, -44, -50, -44, -46, -45, -49, -44, -47, -40, -43, -42, -46, -44, -43, -45, -44, -53, -48, -47, -44, -47, -50, -46, -55, -41, -49, -44, -42, -44, -48, -48, -45, -40, -41, -42, -44, -47, -52, -49, -48, -45, -45, -49, -49, -51, -42, -41, -45, -44, -42, -50, -48, -59, -54, -48, -41, -48, -49, -45, -45, -47, -45, -44]
plt.figure(1)
val=sorted(y1)
std = st.stdev(val)
mean = st.mean(val)
bins=np.linspace(min(val), max(val),50)
plt.hist(val,bins, facecolor='green', alpha=0.75)
y = mlab.normpdf( bins, mean, std)
plt.plot(bins, y, 'r--', linewidth=1)
plt.xlabel ('X Values')
plt.ylabel ('No. of Values')
plt.title ('Histogram')
plt.legend()
plt.show()
I donno whats wrong with the PDF plot.Why is it not following histogram reading?Any help on that would be great.Thanks in advance!
Upvotes: 0
Views: 365
Reputation: 339250
There is nothing wrong with the plot and the pdf is correct.
The bars show the histogram, that is the frequency of the values. The line shows the probability density function. Those are of course on very different scales.
If you want to show both, bars and line, on the same scale, you need to either scale the bars, or the line.
plt.hist(val, bins, density=True, facecolor='green', alpha=0.75, )
or
h, e, _ = plt.hist(val, bins, facecolor='green', alpha=0.75, )
f = np.sum(h*np.diff(e))
y = mlab.normpdf( bins, mean, std)
plt.plot(bins, y*f, 'r--', linewidth=1)
depending on whether you want to show the density or the absolute values.
Upvotes: 2