根据我在@Warren Weckesser的原始答案中的理解,你reffered to "all you need to do"是:
写一个cdf(b) - cdf(a)的近似值作为cdf(b) - cdf(a)= pdf(m)*(b - a)其中m是,例如,区间的中点[a,b] ]
我们可以尝试遵循他的建议,并根据箱子的中心点绘制两种获取pdf值的方法:
具有PDF功能
具有CDF功能:
import numpy as np
import matplotlib.pyplot as plt
from scipy import stats
# generate log-normal distributed set of samples
np.random.seed(42)
samples = np.random.lognormal(mean=1, sigma=.4, size=10000)
N_bins = 50
# make a fit to the samples
shape, loc, scale = stats.lognorm.fit(samples, floc=0)
x_fit = np.linspace(samples.min(), samples.max(), 100)
samples_fit = stats.lognorm.pdf(x_fit, shape, loc=loc, scale=scale)
# plot a histrogram with linear x-axis
fig, (ax1, ax2) = plt.subplots(1,2, figsize=(10,5), gridspec_kw={'wspace':0.2})
counts, bin_edges, ignored = ax1.hist(samples, N_bins, histtype='stepfilled', alpha=0.4,
label='histogram')
# calculate area of histogram (area under PDF should be 1)
area_hist = ((bin_edges[1:] - bin_edges[:-1]) * counts).sum()
# plot fit into histogram
ax1.plot(x_fit, samples_fit*area_hist, label='fitted and area-scaled PDF', linewidth=