:orphan:

Examples gallery
================

Below are examples of the different things you can do with lmfit.
Click on any image to see the complete source code and output.

We encourage users (i.e., YOU) to submit user-guide-style, documented,
and preferably self-contained examples of how you use lmfit for
inclusion in this gallery! Please note that many of the examples
below currently do *not* follow these guidelines yet.



.. raw:: html

    <div class="sphx-glr-thumbnails">

.. thumbnail-parent-div-open

.. raw:: html

    <div class="sphx-glr-thumbcontainer" tooltip="Simple example demonstrating how to read in the data using pandas and supply the elements of the DataFrame to lmfit.">

.. only:: html

  .. image:: /examples/images/thumb/sphx_glr_example_use_pandas_thumb.png
    :alt:

  :ref:`sphx_glr_examples_example_use_pandas.py`

.. raw:: html

      <div class="sphx-glr-thumbnail-title">Fit with Data in a pandas DataFrame</div>
    </div>


.. raw:: html

    <div class="sphx-glr-thumbcontainer" tooltip="ExpressionModels allow a model to be built from a user-supplied expression. See: https://lmfit.github.io/lmfit-py/builtin_models.html#user-defined-models">

.. only:: html

  .. image:: /examples/images/thumb/sphx_glr_example_expression_model_thumb.png
    :alt:

  :ref:`sphx_glr_examples_example_expression_model.py`

.. raw:: html

      <div class="sphx-glr-thumbnail-title">Using an ExpressionModel</div>
    </div>


.. raw:: html

    <div class="sphx-glr-thumbcontainer" tooltip="Sometimes specifying boundaries using min and max are not sufficient, and more complicated (inequality) constraints are needed. In the example below the center of the Lorentzian peak is constrained to be between 0-5 away from the center of the Gaussian peak.">

.. only:: html

  .. image:: /examples/images/thumb/sphx_glr_example_fit_with_inequality_thumb.png
    :alt:

  :ref:`sphx_glr_examples_example_fit_with_inequality.py`

.. raw:: html

      <div class="sphx-glr-thumbnail-title">Fit Using Inequality Constraint</div>
    </div>


.. raw:: html

    <div class="sphx-glr-thumbcontainer" tooltip="This example compares the leastsq and differential_evolution algorithms on a fairly simple problem.">

.. only:: html

  .. image:: /examples/images/thumb/sphx_glr_example_diffev_thumb.png
    :alt:

  :ref:`sphx_glr_examples_example_diffev.py`

.. raw:: html

      <div class="sphx-glr-thumbnail-title">Fit Using differential_evolution Algorithm</div>
    </div>


.. raw:: html

    <div class="sphx-glr-thumbcontainer" tooltip="A major advantage of using lmfit is that one can specify boundaries on fitting parameters, even if the underlying algorithm in SciPy does not support this. For more information on how this is implemented, please refer to: https://lmfit.github.io/lmfit-py/bounds.html">

.. only:: html

  .. image:: /examples/images/thumb/sphx_glr_example_fit_with_bounds_thumb.png
    :alt:

  :ref:`sphx_glr_examples_example_fit_with_bounds.py`

.. raw:: html

      <div class="sphx-glr-thumbnail-title">Fit Using Bounds</div>
    </div>


.. raw:: html

    <div class="sphx-glr-thumbcontainer" tooltip="Fit with Algebraic Constraint">

.. only:: html

  .. image:: /examples/images/thumb/sphx_glr_example_fit_with_algebraic_constraint_thumb.png
    :alt:

  :ref:`sphx_glr_examples_example_fit_with_algebraic_constraint.py`

.. raw:: html

      <div class="sphx-glr-thumbnail-title">Fit with Algebraic Constraint</div>
    </div>


.. raw:: html

    <div class="sphx-glr-thumbcontainer" tooltip="The reduce_fcn specifies how to convert a residual array to a scalar value for the scalar minimizers. The default value is None (i.e., &quot;sum of squares of residual&quot;) - alternatives are: negentropy, neglogcauchy, or a user-specified callable. For more information please refer to: https://lmfit.github.io/lmfit-py/fitting.html#using-the-minimizer-class">

.. only:: html

  .. image:: /examples/images/thumb/sphx_glr_example_reduce_fcn_thumb.png
    :alt:

  :ref:`sphx_glr_examples_example_reduce_fcn.py`

.. raw:: html

      <div class="sphx-glr-thumbnail-title">Fit Specifying Different Reduce Function</div>
    </div>


.. raw:: html

    <div class="sphx-glr-thumbcontainer" tooltip="SymPy is a Python library for symbolic mathematics. It can be very useful to build a model with SymPy and then apply that model to the data with lmfit. This example shows how to do that. Please note that this example requires both the sympy and matplotlib packages.">

.. only:: html

  .. image:: /examples/images/thumb/sphx_glr_example_sympy_thumb.png
    :alt:

  :ref:`sphx_glr_examples_example_sympy.py`

.. raw:: html

      <div class="sphx-glr-thumbnail-title">Building a lmfit model with SymPy</div>
    </div>


.. raw:: html

    <div class="sphx-glr-thumbcontainer" tooltip="This example compares the leastsq and basinhopping algorithms on a decaying sine wave.  Note that this can be used to compare other fitting algorithms too.">

.. only:: html

  .. image:: /examples/images/thumb/sphx_glr_example_basinhopping_thumb.png
    :alt:

  :ref:`sphx_glr_examples_example_basinhopping.py`

.. raw:: html

      <div class="sphx-glr-thumbnail-title">Fit comparing leastsq and basin hopping, or other methods</div>
    </div>


.. raw:: html

    <div class="sphx-glr-thumbcontainer" tooltip="Fitting multiple (simulated) Gaussian data sets simultaneously.">

.. only:: html

  .. image:: /examples/images/thumb/sphx_glr_example_fit_multi_datasets_thumb.png
    :alt:

  :ref:`sphx_glr_examples_example_fit_multi_datasets.py`

.. raw:: html

      <div class="sphx-glr-thumbnail-title">Fit Multiple Data Sets</div>
    </div>


.. raw:: html

    <div class="sphx-glr-thumbcontainer" tooltip="Fitting multiple (simulated) Gaussian data sets simultaneously, using the Model interface.">

.. only:: html

  .. image:: /examples/images/thumb/sphx_glr_example_fit_multi_datasets_Model-interface_thumb.png
    :alt:

  :ref:`sphx_glr_examples_example_fit_multi_datasets_Model-interface.py`

.. raw:: html

      <div class="sphx-glr-thumbnail-title">Fit Multiple Data Sets Using Model Interface</div>
    </div>


.. raw:: html

    <div class="sphx-glr-thumbcontainer" tooltip="This notebook shows a simple example of using the lmfit.Model class. For more information please refer to: https://lmfit.github.io/lmfit-py/model.html#the-model-class.">

.. only:: html

  .. image:: /examples/images/thumb/sphx_glr_example_Model_interface_thumb.png
    :alt:

  :ref:`sphx_glr_examples_example_Model_interface.py`

.. raw:: html

      <div class="sphx-glr-thumbnail-title">Fit using the Model interface</div>
    </div>


.. raw:: html

    <div class="sphx-glr-thumbcontainer" tooltip="Specifying an analytical function to calculate the Jacobian can speed-up the fitting procedure.">

.. only:: html

  .. image:: /examples/images/thumb/sphx_glr_example_fit_with_derivfunc_thumb.png
    :alt:

  :ref:`sphx_glr_examples_example_fit_with_derivfunc.py`

.. raw:: html

      <div class="sphx-glr-thumbnail-title">Fit Specifying a Function to Compute the Jacobian</div>
    </div>


.. raw:: html

    <div class="sphx-glr-thumbcontainer" tooltip="Outliers can sometimes be identified by assessing the influence of each datapoint. To assess the influence of one point, we fit the dataset without the point and compare the result with the fit of the full dataset. The code below shows how to do this with lmfit. Note that the presented method is very basic.">

.. only:: html

  .. image:: /examples/images/thumb/sphx_glr_example_detect_outliers_thumb.png
    :alt:

  :ref:`sphx_glr_examples_example_detect_outliers.py`

.. raw:: html

      <div class="sphx-glr-thumbnail-title">Outlier detection via leave-one-out</div>
    </div>


.. raw:: html

    <div class="sphx-glr-thumbcontainer" tooltip="This examples shows a general way of fitting a model to y(x) data which has uncertainties in both y and x.">

.. only:: html

  .. image:: /examples/images/thumb/sphx_glr_example_uncertainties_y_and_x_thumb.png
    :alt:

  :ref:`sphx_glr_examples_example_uncertainties_y_and_x.py`

.. raw:: html

      <div class="sphx-glr-thumbnail-title">Fitting data with uncertainties in x and y</div>
    </div>


.. raw:: html

    <div class="sphx-glr-thumbcontainer" tooltip="This notebook shows how to fit the parameters of a complex resonator, using lmfit.Model and defining a custom Model class.">

.. only:: html

  .. image:: /examples/images/thumb/sphx_glr_example_complex_resonator_model_thumb.png
    :alt:

  :ref:`sphx_glr_examples_example_complex_resonator_model.py`

.. raw:: html

      <div class="sphx-glr-thumbnail-title">Complex Resonator Model</div>
    </div>


.. raw:: html

    <div class="sphx-glr-thumbcontainer" tooltip="FIXME: this is a useful example; however, it doesn&#x27;t run correctly anymore as the PTSampler was removed in emcee v3...">

.. only:: html

  .. image:: /examples/images/thumb/sphx_glr_lmfit_emcee_model_selection_thumb.png
    :alt:

  :ref:`sphx_glr_examples_lmfit_emcee_model_selection.py`

.. raw:: html

      <div class="sphx-glr-thumbnail-title">Model Selection using lmfit and emcee</div>
    </div>


.. raw:: html

    <div class="sphx-glr-thumbcontainer" tooltip="Providing a function that calculates the Jacobian matrix analytically can reduce the time spent finding a solution. The results from benchmarks comparing two methods (``leastsq`` and least_squares) with and without a function to calculate the Jacobian matrix analytically are presented below.">

.. only:: html

  .. image:: /examples/images/thumb/sphx_glr_example_fit_jacobian_benchmark_thumb.png
    :alt:

  :ref:`sphx_glr_examples_example_fit_jacobian_benchmark.py`

.. raw:: html

      <div class="sphx-glr-thumbnail-title">Benchmarks of methods with and without computing the Jacobian analytically</div>
    </div>


.. raw:: html

    <div class="sphx-glr-thumbcontainer" tooltip="Calculate Confidence Intervals">

.. only:: html

  .. image:: /examples/images/thumb/sphx_glr_example_confidence_interval_thumb.png
    :alt:

  :ref:`sphx_glr_examples_example_confidence_interval.py`

.. raw:: html

      <div class="sphx-glr-thumbnail-title">Calculate Confidence Intervals</div>
    </div>


.. raw:: html

    <div class="sphx-glr-thumbcontainer" tooltip="This example illustrates how to handle two-dimensional data with lmfit.">

.. only:: html

  .. image:: /examples/images/thumb/sphx_glr_example_two_dimensional_peak_thumb.png
    :alt:

  :ref:`sphx_glr_examples_example_two_dimensional_peak.py`

.. raw:: html

      <div class="sphx-glr-thumbnail-title">Fit Two Dimensional Peaks</div>
    </div>


.. raw:: html

    <div class="sphx-glr-thumbcontainer" tooltip="Global minimization using the brute method (a.k.a. grid search)">

.. only:: html

  .. image:: /examples/images/thumb/sphx_glr_example_brute_thumb.png
    :alt:

  :ref:`sphx_glr_examples_example_brute.py`

.. raw:: html

      <div class="sphx-glr-thumbnail-title">Global minimization using the brute method (a.k.a. grid search)</div>
    </div>


.. thumbnail-parent-div-close

.. raw:: html

    </div>


.. toctree::
   :hidden:

   /examples/example_use_pandas
   /examples/example_expression_model
   /examples/example_fit_with_inequality
   /examples/example_diffev
   /examples/example_fit_with_bounds
   /examples/example_fit_with_algebraic_constraint
   /examples/example_reduce_fcn
   /examples/example_sympy
   /examples/example_basinhopping
   /examples/example_fit_multi_datasets
   /examples/example_fit_multi_datasets_Model-interface
   /examples/example_Model_interface
   /examples/example_fit_with_derivfunc
   /examples/example_detect_outliers
   /examples/example_uncertainties_y_and_x
   /examples/example_complex_resonator_model
   /examples/lmfit_emcee_model_selection
   /examples/example_fit_jacobian_benchmark
   /examples/example_confidence_interval
   /examples/example_two_dimensional_peak
   /examples/example_brute


.. only:: html

  .. container:: sphx-glr-footer sphx-glr-footer-gallery

    .. container:: sphx-glr-download sphx-glr-download-python

      :download:`Download all examples in Python source code: examples_python.zip </examples/examples_python.zip>`

    .. container:: sphx-glr-download sphx-glr-download-jupyter

      :download:`Download all examples in Jupyter notebooks: examples_jupyter.zip </examples/examples_jupyter.zip>`


.. only:: html

 .. rst-class:: sphx-glr-signature

    `Gallery generated by Sphinx-Gallery <https://sphinx-gallery.github.io>`_
