import numpy as np from lmfit.old_models1d import GaussianModel import matplotlib.pyplot as plt data = np.loadtxt('model1d_gauss.dat') x = data[:, 0] y = data[:, 1] model = GaussianModel() model.guess_starting_values(y, x=x) # model.params['amplitude'].value=6.0 init_fit = model.model(x=x) model.fit(y, x=x) print model.fit_report(min_correl=0.25) final_fit = model.model(x=x) plt.plot(x, final_fit, 'r-') plt.plot(x, init_fit, 'k--') plt.plot(x, y, 'bo') plt.show()
eps = 0.15 off = 9 slo = 0.0012 sca = 1./(2.0*np.sqrt(2*np.pi))/sig noise = eps*np.random.randn(len(x)) dat = off +slo*x + amp*sca* np.exp(-(x-cen)**2 / (2*sig)**2) + noise # mod = ExponentialModel(background='linear') # mod = LinearModel() mod = GaussianModel(background='quad') mod = VoigtModel(background='quad') mod = LorenztianModel(background='quad') mod.guess_starting_values(dat, x, negative=False) mod.params['bkg_offset'].value=min(dat) init = mod.model(x=x)+mod.calc_background(x) mod.fit(dat, x=x) print mod.fit_report() fit = mod.model(x=x)+mod.calc_background(x) plt.plot(x, dat) plt.plot(x, init) plt.plot(x, fit) plt.show()