.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "intro/scipy/auto_examples/solutions/plot_curvefit_temperature_data.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end <sphx_glr_download_intro_scipy_auto_examples_solutions_plot_curvefit_temperature_data.py>` to download the full example code. .. rst-class:: sphx-glr-example-title .. _sphx_glr_intro_scipy_auto_examples_solutions_plot_curvefit_temperature_data.py: ============================================================== Curve fitting: temperature as a function of month of the year ============================================================== We have the min and max temperatures in Alaska for each months of the year. We would like to find a function to describe this yearly evolution. For this, we will fit a periodic function. .. GENERATED FROM PYTHON SOURCE LINES 13-15 The data ########################################################### .. GENERATED FROM PYTHON SOURCE LINES 15-28 .. code-block:: Python import numpy as np temp_max = np.array([17, 19, 21, 28, 33, 38, 37, 37, 31, 23, 19, 18]) temp_min = np.array([-62, -59, -56, -46, -32, -18, -9, -13, -25, -46, -52, -58]) import matplotlib.pyplot as plt months = np.arange(12) plt.plot(months, temp_max, "ro") plt.plot(months, temp_min, "bo") plt.xlabel("Month") plt.ylabel("Min and max temperature") .. image-sg:: /intro/scipy/auto_examples/solutions/images/sphx_glr_plot_curvefit_temperature_data_001.png :alt: plot curvefit temperature data :srcset: /intro/scipy/auto_examples/solutions/images/sphx_glr_plot_curvefit_temperature_data_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none Text(35.472222222222214, 0.5, 'Min and max temperature') .. GENERATED FROM PYTHON SOURCE LINES 29-31 Fitting it to a periodic function ########################################################### .. GENERATED FROM PYTHON SOURCE LINES 31-42 .. code-block:: Python import scipy as sp def yearly_temps(times, avg, ampl, time_offset): return avg + ampl * np.cos((times + time_offset) * 2 * np.pi / times.max()) res_max, cov_max = sp.optimize.curve_fit(yearly_temps, months, temp_max, [20, 10, 0]) res_min, cov_min = sp.optimize.curve_fit(yearly_temps, months, temp_min, [-40, 20, 0]) .. GENERATED FROM PYTHON SOURCE LINES 43-45 Plotting the fit ########################################################### .. GENERATED FROM PYTHON SOURCE LINES 45-57 .. code-block:: Python days = np.linspace(0, 12, num=365) plt.figure() plt.plot(months, temp_max, "ro") plt.plot(days, yearly_temps(days, *res_max), "r-") plt.plot(months, temp_min, "bo") plt.plot(days, yearly_temps(days, *res_min), "b-") plt.xlabel("Month") plt.ylabel(r"Temperature ($^\circ$C)") plt.show() .. image-sg:: /intro/scipy/auto_examples/solutions/images/sphx_glr_plot_curvefit_temperature_data_002.png :alt: plot curvefit temperature data :srcset: /intro/scipy/auto_examples/solutions/images/sphx_glr_plot_curvefit_temperature_data_002.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.100 seconds) .. _sphx_glr_download_intro_scipy_auto_examples_solutions_plot_curvefit_temperature_data.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_curvefit_temperature_data.ipynb <plot_curvefit_temperature_data.ipynb>` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_curvefit_temperature_data.py <plot_curvefit_temperature_data.py>` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_curvefit_temperature_data.zip <plot_curvefit_temperature_data.zip>` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery <https://sphinx-gallery.github.io>`_