File: doc_builtinmodels_peakmodels.py

package info (click to toggle)
lmfit-py 0.9.11%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 2,908 kB
  • sloc: python: 10,121; makefile: 114; sh: 55
file content (45 lines) | stat: -rw-r--r-- 1,073 bytes parent folder | download
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#!/usr/bin/env python

# <examples/doc_builtinmodels_peakmodels.py>
import matplotlib.pyplot as plt
from numpy import loadtxt

from lmfit.models import GaussianModel, LorentzianModel, VoigtModel

data = loadtxt('test_peak.dat')
x = data[:, 0]
y = data[:, 1]

gamma_free = False

MODEL = 'gauss'
# MODEL = 'loren'
# MODEL = 'voigt'
# gamma_free = True

if MODEL.lower().startswith('g'):
    mod = GaussianModel()
    gamma_free = False
    figname = '../doc/_images/models_peak1.png'
elif MODEL.lower().startswith('l'):
    mod = LorentzianModel()
    gamma_free = False
    figname = '../doc/_images/models_peak2.png'
elif MODEL.lower().startswith('v'):
    mod = VoigtModel()
    figname = '../doc/_images/models_peak3.png'

pars = mod.guess(y, x=x)

if gamma_free:
    pars['gamma'].set(value=0.7, vary=True, expr='')
    figname = '../doc/_images/models_peak4.png'

out = mod.fit(y, pars, x=x)
print(out.fit_report(min_correl=0.25))

plt.plot(x, y, 'b-')
plt.plot(x, out.best_fit, 'r-')
# plt.savefig(figname)
plt.show()
# <end examples/doc_builtinmodels_peakmodels.py>