Fitting math functions to histograms

I would like to fit a gaussian to a histogram and then overplot it. I can
write the code to do this but most plotting packages support such fitting.
However I can't find it for pyplot even after scanning documentation,
googling, etc. In fact, the only fitting functionality I could find was the
polynomial fitting for numpy that is layered underneath matplotlib, i.e.
Numpy.polyfit(...).

Does anyone know if/how this might be built into matplotlib?

Thanks,
Bill

For a Gaussian distribution, the best fit is provided by the normal
distribution which has the same mean and stddev as your empirical data
(this is not true in general for other distributions). Once you have
the mean and stddev from the data, you can use normpdf to plot the
analytic density -- see for example

http://matplotlib.sourceforge.net/search.html?q=normpdf

For more powerful density fitting and sampling, see scipy.stats
functions, eg scipy.stats.norm.fit

JDH

···

On Mon, Nov 30, 2009 at 6:44 PM, William Carithers <wccarithers@...1352...> wrote:

I would like to fit a gaussian to a histogram and then overplot it. I can
write the code to do this but most plotting packages support such fitting.
However I can't find it for pyplot even after scanning documentation,
googling, etc. In fact, the only fitting functionality I could find was the
polynomial fitting for numpy that is layered underneath matplotlib, i.e.
Numpy.polyfit(...).

Does anyone know if/how this might be built into matplotlib?

Hi John,

Yes, that is true if the data is truly gaussian. In my case, I know that the
data have non-gaussian tails which tend to dominate the calculation of the
standard deviation. I should have been clearer in my post that what I
actually wanted to do was fit a gaussian to the truncated "central" part of
the distribution so that I was not so sensitive to the tails. A better
statement of the problem is that I would like to fit a gaussian to the part
of the data that I suspect is actually gaussian while ignoring the part that
isn't. Unfortunately, if I calculate the standard deviation for the
truncated distribution, then I will underestimate the "sigma" parameter of
the gaussian needed to get a good fit.

I'll take at the scipy.stats.norm . Thanks for your help.
Bill

···

On 11/30/09 7:22 PM, "John Hunter" <jdh2358@...287...> wrote:

On Mon, Nov 30, 2009 at 6:44 PM, William Carithers <wccarithers@...1352...> > wrote:

I would like to fit a gaussian to a histogram and then overplot it. I can
write the code to do this but most plotting packages support such fitting.
However I can't find it for pyplot even after scanning documentation,
googling, etc. In fact, the only fitting functionality I could find was the
polynomial fitting for numpy that is layered underneath matplotlib, i.e.
Numpy.polyfit(...).

Does anyone know if/how this might be built into matplotlib?

For a Gaussian distribution, the best fit is provided by the normal
distribution which has the same mean and stddev as your empirical data
(this is not true in general for other distributions). Once you have
the mean and stddev from the data, you can use normpdf to plot the
analytic density -- see for example

Search — Matplotlib 3.8.2 documentation

For more powerful density fitting and sampling, see scipy.stats
functions, eg scipy.stats.norm.fit

JDH