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()
在调用plt.hist()
,不要使用weights=np.ones_like(data)/len(data)
,而应使用weights=np.ones_like(data)/len(data)
normed=True
参数:
plt.hist(data, bins = 20, color = 'w', edgecolor = 'black', alpha = 0.5, normed = True)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.