I want to convert fitted distribution to frequency.
import numpy as np
import matplotlib.pyplot as plt
from scipy import stats
%matplotlib notebook
# sample data generation
np.random.seed(42)
data = sorted(stats.lognorm.rvs(s=0.5, loc=1, scale=1000, size=1000))
# fit lognormal distribution
shape, loc, scale = stats.lognorm.fit(data, loc=0)
pdf_lognorm = stats.lognorm.pdf(data, shape, loc, scale)
fig, ax = plt.subplots(figsize=(8, 4))
ax.hist(data, bins='auto', density=True)
ax.plot(data, pdf_lognorm)
ax.set_ylabel('probability')
ax.set_title('Linear Scale')
The above code snippet will generate the following plot:
As you can see, the y-axis is in terms of probability. But I want it to be in terms of frequencies.
fig, ax = plt.subplots(figsize=(8, 4))
ax.hist(data, bins='auto')
ax.set_ylabel('probability')
ax.set_title('Linear Scale')
By unsetting density=True
, the histogram is displayed in terms of frequencies. But I do not know how to fit the distribution in the same way as it is in histograms - observe how I couldn't draw the orange fitted line in this histogram.
How can I do this? I think I should multiply the fitted distribution with the area under the curve of the histogram, but I don't know how to.