Reputation: 169
I'm working on some time series data and I tried to fit the data with a gamma distribution. But the problem is that the magnitude of fitted pdf is much lower than that of the histogram. Here are my code and plot. What is wrong with the plot?
# the data
data = contents[0][1:]
# normalized histogram
weights = np.ones_like(data)/len(data)
plt.hist(data, bins = 20, color = 'w', edgecolor = 'black', alpha = 0.5, weights = weights)
# fit with gamma distribution and plot the pdf
dist = getattr(scipy.stats, 'gamma')
param = dist.fit(data)
x = np.linspace(min(data), max(data), 100)
pdf_fit = dist.pdf(x, *param[:-2], loc = param[-2], scale = param[-1])
plt.plot(x, pdf_fit/sum(pdf_fit), label = 'Gamma')
plt.legend(loc = 'upper right')
plt.show()
Upvotes: 0
Views: 691
Reputation: 114821
In your call to plt.hist()
, instead of using weights=np.ones_like(data)/len(data)
, use the argument normed=True
:
plt.hist(data, bins = 20, color = 'w', edgecolor = 'black', alpha = 0.5, normed = True)
Upvotes: 1