Difference scipy.stats.norm mlab.normpdf and setting gaussian

This relates to the following question:

Setting a bar chart using python

There joaquin offers installation using

# best fit of data
(mu, sigma) = norm.fit(datos)
# the histogram of the data
n, bins, patches = plt.hist(datos, 60, normed=1)

# a# the histogram of the data
n, bins, patches = plt.hist(datos, 60, normed=1)

# add a 'best fit' line
y = mlab.normpdf( bins, mu, sigma)
l = plt.plot(bins, y)dd a 'best fit' line
y = mlab.normpdf( bins, mu, sigma)
l = plt.plot(bins, y)

I also tried out the Ralph version - just bind gausses to bin data.

def plotter(lightcurves_red):

    fitfunc  = lambda p, x: p[0]*np.exp(-0.5*((x-p[1])/p[2])**2)
    errfunc  = lambda p, x, y: (y - fitfunc(p, x))

    gs = gridspec.GridSpec(1,1) #in reality i have a couple of these, hence the gridspec
    ax0 = plt.subplot(gs[0]) 

    #method one using mlab.normpdf()
    n_red, bins_red, patches_red = ax0.hist(lightcurves_red, bins='fd', normed=True,facecolor=sns.xkcd_rgb["blood red"])
    (mu_red, sigma_red) = norm.fit(lightcurves_red)
    y_red = mlab.normpdf( bins_red, mu_red, sigma_red)
    l_red = ax0.plot(bins_red, y_red, 'b--', linewidth=2)


    ##method two, just fitting a gauss with 3 params
    init  = [1.0, 0.5, 0.5]
    out_red   = leastsq( errfunc, init, args=( bins_red[0:len(bins_red)-1],n_red))
    c_red = out_red[0]
    ax0.plot(bins_red[0:len(bins_red)-1], fitfunc(c_red, bins_red[0:len(bins_red)-1]),color='black') 

    ax0.annotate(r'$\sigma = $' + str(abs(np.round(c_red[2],3))), xy=(0,5),xytext=(-0.075,1),fontsize=25)


    #plot
    #plot formatting....

    plt.show()

This gives me two very different tricks. Why is this? enter image description here

0
source share

Source: https://habr.com/ru/post/1527629/


All Articles