1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
| #!/usr/bin/env python
# -*- coding:Utf-8 -*-
import numpy as np
import matplotlib.pyplot as plt
from scipy.optimize import curve_fit
def gaussian(x, a, b, c):
return a * np.exp(-(x - b)**2 / c**2)
if __name__ == '__main__':
data = np.dtype( [ ('distance',np.int), ('effectif',np.int) ] )
enreg = np.loadtxt('tmp.txt', dtype=data, skiprows=True)
# Estimation des paramètres
amplitude, position, largeur = 18, 4, 2
params = [ amplitude, position, largeur ]
# Fit
popt, pcov = curve_fit(gaussian, enreg['distance'], enreg['effectif'], p0=params)
# Print results
print "amplitude = %.3f +/- %.3f" % (popt[0], np.sqrt(pcov[0, 0]))
print "position = %.3f +/- %.3f" % (popt[1], np.sqrt(pcov[1, 1]))
print "largeur = %.3f +/- %.3f" % (popt[2], np.sqrt(pcov[2, 2]))
# Plot model
xm= np.linspace(enreg['distance'][0],enreg['distance'][-1], 512)
plt.plot(xm, gaussian(xm, popt[0], popt[1], popt[2]), color='r', linewidth=2.0)
plt.bar( enreg['distance'], enreg['effectif'])
plt.show() |
Partager