
.. DO NOT EDIT.
.. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY.
.. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE:
.. "gallery/images_contours_and_fields/specgram_demo.py"
.. LINE NUMBERS ARE GIVEN BELOW.

.. only:: html

    .. note::
        :class: sphx-glr-download-link-note

        Click :ref:`here <sphx_glr_download_gallery_images_contours_and_fields_specgram_demo.py>`
        to download the full example code

.. rst-class:: sphx-glr-example-title

.. _sphx_glr_gallery_images_contours_and_fields_specgram_demo.py:


================
Spectrogram Demo
================

Demo of a spectrogram plot (`~.axes.Axes.specgram`).

.. GENERATED FROM PYTHON SOURCE LINES 8-39

.. code-block:: default

    import matplotlib.pyplot as plt
    import numpy as np

    # Fixing random state for reproducibility
    np.random.seed(19680801)

    dt = 0.0005
    t = np.arange(0.0, 20.0, dt)
    s1 = np.sin(2 * np.pi * 100 * t)
    s2 = 2 * np.sin(2 * np.pi * 400 * t)

    # create a transient "chirp"
    s2[t <= 10] = s2[12 <= t] = 0

    # add some noise into the mix
    nse = 0.01 * np.random.random(size=len(t))

    x = s1 + s2 + nse  # the signal
    NFFT = 1024  # the length of the windowing segments
    Fs = int(1.0 / dt)  # the sampling frequency

    fig, (ax1, ax2) = plt.subplots(nrows=2)
    ax1.plot(t, x)
    Pxx, freqs, bins, im = ax2.specgram(x, NFFT=NFFT, Fs=Fs, noverlap=900)
    # The `specgram` method returns 4 objects. They are:
    # - Pxx: the periodogram
    # - freqs: the frequency vector
    # - bins: the centers of the time bins
    # - im: the .image.AxesImage instance representing the data in the plot
    plt.show()




.. image:: /gallery/images_contours_and_fields/images/sphx_glr_specgram_demo_001.png
    :alt: specgram demo
    :class: sphx-glr-single-img





.. GENERATED FROM PYTHON SOURCE LINES 40-47

------------

References
""""""""""

The use of the following functions, methods is shown
in this example:

.. GENERATED FROM PYTHON SOURCE LINES 48-52

.. code-block:: default


    import matplotlib
    matplotlib.axes.Axes.specgram
    matplotlib.pyplot.specgram




.. rst-class:: sphx-glr-script-out

 Out:

 .. code-block:: none


    <function specgram at 0x7f73be8293a0>




.. rst-class:: sphx-glr-timing

   **Total running time of the script:** ( 0 minutes  1.571 seconds)


.. _sphx_glr_download_gallery_images_contours_and_fields_specgram_demo.py:


.. only :: html

 .. container:: sphx-glr-footer
    :class: sphx-glr-footer-example



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

     :download:`Download Python source code: specgram_demo.py <specgram_demo.py>`



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

     :download:`Download Jupyter notebook: specgram_demo.ipynb <specgram_demo.ipynb>`


.. only:: html

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

    Keywords: matplotlib code example, codex, python plot, pyplot
    `Gallery generated by Sphinx-Gallery
    <https://sphinx-gallery.readthedocs.io>`_
