.. 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_periodicity_finder.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code .. rst-class:: sphx-glr-example-title .. _sphx_glr_intro_scipy_auto_examples_solutions_plot_periodicity_finder.py: ========================== Crude periodicity finding ========================== Discover the periods in evolution of animal populations (:download:`../../../../data/populations.txt`) .. GENERATED FROM PYTHON SOURCE LINES 11-13 Load the data ########################################################### .. GENERATED FROM PYTHON SOURCE LINES 13-19 .. code-block:: default import numpy as np data = np.loadtxt('../../../../data/populations.txt') years = data[:, 0] populations = data[:, 1:] .. GENERATED FROM PYTHON SOURCE LINES 20-22 Plot the data ########################################################### .. GENERATED FROM PYTHON SOURCE LINES 22-30 .. code-block:: default import matplotlib.pyplot as plt plt.figure() plt.plot(years, populations * 1e-3) plt.xlabel('Year') plt.ylabel(r'Population number ($\cdot10^3$)') plt.legend(['hare', 'lynx', 'carrot'], loc=1) .. image-sg:: /intro/scipy/auto_examples/solutions/images/sphx_glr_plot_periodicity_finder_001.png :alt: plot periodicity finder :srcset: /intro/scipy/auto_examples/solutions/images/sphx_glr_plot_periodicity_finder_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none .. GENERATED FROM PYTHON SOURCE LINES 31-33 Plot its periods ########################################################### .. GENERATED FROM PYTHON SOURCE LINES 33-47 .. code-block:: default import scipy as sp ft_populations = sp.fftpack.fft(populations, axis=0) frequencies = sp.fftpack.fftfreq(populations.shape[0], years[1] - years[0]) periods = 1 / frequencies plt.figure() plt.plot(periods, abs(ft_populations) * 1e-3, 'o') plt.xlim(0, 22) plt.xlabel('Period') plt.ylabel(r'Power ($\cdot10^3$)') plt.show() .. image-sg:: /intro/scipy/auto_examples/solutions/images/sphx_glr_plot_periodicity_finder_002.png :alt: plot periodicity finder :srcset: /intro/scipy/auto_examples/solutions/images/sphx_glr_plot_periodicity_finder_002.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none /opt/build/repo/intro/scipy/examples/solutions/plot_periodicity_finder.py:37: RuntimeWarning: divide by zero encountered in divide periods = 1 / frequencies .. GENERATED FROM PYTHON SOURCE LINES 48-51 There's probably a period of around 10 years (obvious from the plot), but for this crude a method, there's not enough data to say much more. .. rst-class:: sphx-glr-timing **Total running time of the script:** ( 0 minutes 0.118 seconds) .. _sphx_glr_download_intro_scipy_auto_examples_solutions_plot_periodicity_finder.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_periodicity_finder.py ` .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_periodicity_finder.ipynb ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_