.. note:: :class: sphx-glr-download-link-note Click :ref:`here ` to download the full example code .. rst-class:: sphx-glr-example-title .. _sphx_glr_examples_documentation_model_two_components.py: doc_model_two_components.py =========================== .. image:: /examples/documentation/images/sphx_glr_model_two_components_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out Out: .. code-block:: none [[Model]] (Model(gaussian) + Model(line)) [[Fit Statistics]] # fitting method = leastsq # function evals = 44 # data points = 101 # variables = 5 chi-square = 2.57855517 reduced chi-square = 0.02685995 Akaike info crit = -360.457020 Bayesian info crit = -347.381417 [[Variables]] amp: 8.45931062 +/- 0.12414515 (1.47%) (init = 5) cen: 5.65547873 +/- 0.00917678 (0.16%) (init = 5) wid: 0.67545524 +/- 0.00991686 (1.47%) (init = 1) slope: 0.26484404 +/- 0.00574892 (2.17%) (init = 0) intercept: -0.96860202 +/- 0.03352202 (3.46%) (init = 1) [[Correlations]] (unreported correlations are < 0.100) C(slope, intercept) = -0.795 C(amp, wid) = 0.666 C(amp, intercept) = -0.222 C(amp, slope) = -0.169 C(cen, slope) = -0.162 C(wid, intercept) = -0.148 C(cen, intercept) = 0.129 C(wid, slope) = -0.113 | .. code-block:: default ## import warnings warnings.filterwarnings("ignore") ## # import matplotlib.pyplot as plt from numpy import exp, loadtxt, pi, sqrt from lmfit import Model data = loadtxt('model1d_gauss.dat') x = data[:, 0] y = data[:, 1] + 0.25*x - 1.0 def gaussian(x, amp, cen, wid): """1-d gaussian: gaussian(x, amp, cen, wid)""" return (amp / (sqrt(2*pi) * wid)) * exp(-(x-cen)**2 / (2*wid**2)) def line(x, slope, intercept): """a line""" return slope*x + intercept mod = Model(gaussian) + Model(line) pars = mod.make_params(amp=5, cen=5, wid=1, slope=0, intercept=1) result = mod.fit(y, pars, x=x) print(result.fit_report()) plt.plot(x, y, 'bo') plt.plot(x, result.init_fit, 'k--', label='initial fit') plt.plot(x, result.best_fit, 'r-', label='best fit') plt.legend(loc='best') plt.show() # .. rst-class:: sphx-glr-timing **Total running time of the script:** ( 0 minutes 0.142 seconds) .. _sphx_glr_download_examples_documentation_model_two_components.py: .. only :: html .. container:: sphx-glr-footer :class: sphx-glr-footer-example .. container:: sphx-glr-download :download:`Download Python source code: model_two_components.py ` .. container:: sphx-glr-download :download:`Download Jupyter notebook: model_two_components.ipynb ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_