erdogant/distfit

A code snippet for `plotting` the best fit and the original data

luo-lorry opened this issue · 3 comments

Thank you for this useful and practical package! Would it be possible to include the feature to plot the best-fit model over the original data? I hope my tentative solution might help other users.

The best-fit model and the actual data can be plotted using the code below.

from distfit import distfit

fig, ax = plt.subplots()
dist = distfit(alpha=0.05, smooth=10)
dist.fit_transform(data)
best_distr = dist.model
exec(f"from scipy.stats import {best_distr['name']}")
ax.hist(data, density=True, label="data")
data_range = np.linspace(data.min(), data.max())
ax.plot(data_range, getattr(scipy.stats, best_distr['name']).pdf(data_range, *best_distr['params']), color='r', lw=4, label=f"{best_distr['name']} ({best_distr['stats']}={best_distr['score']:.3f})")
ax.legend()

download

Thank you for the suggestion!
I recently created an update in the plots. Creating Pareto plots is now possible!
Checkout the documentation pages for details.

Your figure can be created by customizing the different elements on the plot.

fig, ax = dfit.plot(chart='PDF',
                pdf_properties={'color': 'r', 'linewidth': 3},
                cii_properties=None,
                emp_properties={'linewidth': 1},
                bar_properties={'color': '#1e3f5a'})

Let me know if something is not clear.

Update to the latest version with:
pip install -U distfit

image

I appreciate your prompt reply, and I'll give it a shot. A follow-up question, why do the fitted curves (other than the best fit beta) look more like CDF rather than PDF in this plot?

True. Because I accidentally copy-paste the wrong one. It is now possible to also plot the CDF using fig, ax = dfit.plot(chart='CDF') Here is an example from the documentation pages for only the PDF and histogram.

image