En pdf:
Ajuste a un histograma
from scipy.optimize import curve_fit
hist, bin_edges = np.histogram(datos, density=False)
bin_centres = (bin_edges[:-1] + bin_edges[1:])/2
# Define model function to be used to fit to the data above:
def gauss(x, *p):
A, mu, sigma = p
return A*np.exp(-(x-mu)**2/(2.*sigma**2))
# p0 is the initial guess for the fitting coefficients (A, mu and sigma above)
p0 = [1., 0., 1.]
coeff, var_matrix = curve_fit(gauss, bin_centres, hist, p0=p0)
# Get the fitted curve
hist_fit = gauss(bin_centres, *coeff)
plt.bar(bin_centres, hist, label='Test data')
plt.plot(bin_centres, hist_fit, label='Fitted data', color='r')
# Finally, lets get the fitting parameters, i.e. the mean and standard deviation:
print('media = ', coeff[1])
print('desviación estandar = ', np.abs(coeff[2]))
plt.savefig('./test.png')
plt.show()