Created
January 29, 2016 14:37
-
-
Save habi/66be86bfb94a96a51245 to your computer and use it in GitHub Desktop.
Gaussian fit, based on http://stackoverflow.com/a/11507723/323100
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy | |
from scipy.optimize import curve_fit | |
import matplotlib.pyplot as plt | |
# Define model function to be used to fit to the data above: | |
def gauss(x, *p): | |
A, mu, sigma = p | |
return A*numpy.exp(-(x-mu)**2/(2.*sigma**2)) | |
xdata = numpy.linspace(-5,5,100) | |
y = gauss(xdata,2,2,0.9) | |
ydata = y + 0.1 * numpy.random.normal(size=len(xdata)) | |
# Fit | |
# p0 is the initial guess for the fitting coefficients (A, mu and sigma above) | |
p0 = [1, 0, 1] | |
coeff, var_matrix = curve_fit(gauss, xdata, ydata, p0=p0) | |
plt.plot(ydata, label='Test data') | |
plt.plot(gauss(xdata, *coeff), label='Gaussian fit') | |
plt.legend(loc='best') | |
plt.show() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment