lmfit fits 'wrong' peak in data with multiple peaks

348 views Asked by At

With this code (before the snippet I just read in the data, which works fine, and after the snippet I just do labels etc.)

plt.errorbar(xdata, ydata, yerr, fmt='.', label='Data')

model = models.GaussianModel()
params = model.make_params()

params['center'].set(6.5)
#params['center'].vary = False

fit = model.fit(ydata, params=params, x=xdata, weights=1/yerr)
print(fit.fit_report())

plt.plot(xdata, fit.best_fit, label='Fit')

I try to fit the last peak (approximately at x=6.5). But as you can see in the picture, the code does not do that. Can anyone tell me why that is?

enter image description here

Edit: If I run the line params['center'].vary = False the "fit" just becomes zero everywhere.

1

There are 1 answers

0
Ch L On BEST ANSWER

I have never used lmfit, but the problem is most likely, that you try to fit the whole data region. Considering the entire region of data you handed to the .fit call, the resulting fit is probably the best and correct.
You should try to pass only relevant data to the fit. In your case xdata should only be the set of data points from around 5.5 up to 7.5 (or somewhere around these numbers). ydata has to be adapted to these values as well, of course. Then the fit should work nicely.