Search code examples
pythonhistogramgaussiancurve

Same height of HISTOGRAM and a DIFFERENT gaussian curve, not related to histogram data


I have one data something like this, but with 1000 points: fractions = [ 85,86,78,89,73,80,85,79,......83] (we could assume a 1000 random points between (70,90)

(edit: I want the maximum height of the Gaussian the be the same as maximum height of histogram.)

And I want to compare this with a gaussian curve given by

def quasars_gaussian_curve(mu= 45,sigma=0.35):

    x = np.linspace(mu - 3*sigma, mu + 3*sigma, 100)
    plt.plot(x, stats.norm.pdf(x, mu, sigma)

So when I do this: quasars_gaussian_curve(mu, sigma))

plt.hist(fraction,density = 1) 
plt.show()

I get this :

! [Histogram vs gaussian: I need the gaussian to have the same height as my histogram] https://i.sstatic.net/CMiLM.jpg


Solution

  • Ok. So there's this astropy package for Python. Didn't know it had this Gaussian1D class in astro fitting and modeling. If anyone runs into same problems, even if it's not astronomy and wants to fit a Gaussian curve to their given Amplitude, Mean and StandardDeviataion this is the perfect solution probably. And probably the shortest

    Here's how I did it.

    from astropy.modeling.models import Gaussian1D
    
    def fit_gaussian_curve():
         g1 = Gaussian1D(0.11,44,0.35 )
         x = np.linspace(44 - 3*0.35, 44 + 3*0.35, 100)
         plt.plot(x,g1(x))
    

    (The Max height of histogram was around 0.11, mean 44 and sigma 0.35) And here is my plot now. Compare it to the previous, mission complete.

    !https://i.sstatic.net/ZI6lr.jpg More info here: http://docs.astropy.org/en/stable/modeling/