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

.. only:: html

    .. meta::
        :keywords: codex

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

        :ref:`Go to the end <sphx_glr_download_gallery_animation_animated_histogram.py>`
        to download the full example code.

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

.. _sphx_glr_gallery_animation_animated_histogram.py:


==================
Animated histogram
==================

Use histogram's `.BarContainer` to draw a bunch of rectangles for an animated
histogram.

.. GENERATED FROM PYTHON SOURCE LINES 9-26

.. code-block:: Python


    import functools

    import matplotlib.pyplot as plt
    import numpy as np

    import matplotlib.animation as animation

    # Setting up a random number generator with a fixed state for reproducibility.
    rng = np.random.default_rng(seed=19680801)
    # Fixing bin edges.
    HIST_BINS = np.linspace(-4, 4, 100)

    # Histogram our data with numpy.
    data = rng.standard_normal(1000)
    n, _ = np.histogram(data, HIST_BINS)








.. GENERATED FROM PYTHON SOURCE LINES 27-30

To animate the histogram, we need an ``animate`` function, which generates
a random set of numbers and updates the heights of rectangles. The ``animate``
function updates the `.Rectangle` patches on an instance of `.BarContainer`.

.. GENERATED FROM PYTHON SOURCE LINES 30-41

.. code-block:: Python



    def animate(frame_number, bar_container):
        # Simulate new data coming in.
        data = rng.standard_normal(1000)
        n, _ = np.histogram(data, HIST_BINS)
        for count, rect in zip(n, bar_container.patches):
            rect.set_height(count)

        return bar_container.patches








.. GENERATED FROM PYTHON SOURCE LINES 42-46

Using :func:`~matplotlib.pyplot.hist` allows us to get an instance of
`.BarContainer`, which is a collection of `.Rectangle` instances.  Since
`.FuncAnimation` will only pass the frame number parameter to the animation
function, we use `functools.partial` to fix the ``bar_container`` parameter.

.. GENERATED FROM PYTHON SOURCE LINES 46-58

.. code-block:: Python


    # Output generated via `matplotlib.animation.Animation.to_jshtml`.

    fig, ax = plt.subplots()
    _, _, bar_container = ax.hist(data, HIST_BINS, lw=1,
                                  ec="yellow", fc="green", alpha=0.5)
    ax.set_ylim(top=55)  # set safe limit to ensure that all data is visible.

    anim = functools.partial(animate, bar_container=bar_container)
    ani = animation.FuncAnimation(fig, anim, 50, repeat=False, blit=True)
    plt.show()




.. image-sg:: /gallery/animation/images/sphx_glr_animated_histogram_001.png
   :alt: animated histogram
   :srcset: /gallery/animation/images/sphx_glr_animated_histogram_001.png, /gallery/animation/images/sphx_glr_animated_histogram_001_2_00x.png 2.00x
   :class: sphx-glr-single-img





.. GENERATED FROM PYTHON SOURCE LINES 59-60

.. tags:: plot-type: histogram, animation


.. _sphx_glr_download_gallery_animation_animated_histogram.py:

.. only:: html

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

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

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

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

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

    .. container:: sphx-glr-download sphx-glr-download-zip

      :download:`Download zipped: animated_histogram.zip <animated_histogram.zip>`


.. only:: html

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

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