.. only:: html
.. note::
:class: sphx-glr-download-link-note
Click :ref:`here ` 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.
.. code-block:: default
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation
# Fixing random state for reproducibility
np.random.seed(19680801)
# Fixing bin edges
HIST_BINS = np.linspace(-4, 4, 100)
# histogram our data with numpy
data = np.random.randn(1000)
n, _ = np.histogram(data, HIST_BINS)
To animate the histogram, we need an ``animate`` function, which generates
a random set of numbers and updates the heights of rectangles. We utilize a
python closure to track an instance of `.BarContainer` whose `.Rectangle`
patches we shall update.
.. code-block:: default
def prepare_animation(bar_container):
def animate(frame_number):
# simulate new data coming in
data = np.random.randn(1000)
n, _ = np.histogram(data, HIST_BINS)
for count, rect in zip(n, bar_container.patches):
rect.set_height(count)
return bar_container.patches
return animate
Using :func:`~matplotlib.pyplot.hist` allows us to get an instance of
`.BarContainer`, which is a collection of `.Rectangle` instances. Calling
``prepare_animation`` will define ``animate`` function working with supplied
`.BarContainer`, all this is used to setup `.FuncAnimation`.
.. code-block:: default
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.
ani = animation.FuncAnimation(fig, prepare_animation(bar_container), 50,
repeat=False, blit=True)
plt.show()
.. only:: builder_html
.. container:: sphx-glr-animation
.. raw:: html
.. rst-class:: sphx-glr-timing
**Total running time of the script:** ( 0 minutes 9.888 seconds)
.. _sphx_glr_download_gallery_animation_animated_histogram.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: animated_histogram.py `
.. container:: sphx-glr-download sphx-glr-download-jupyter
:download:`Download Jupyter notebook: animated_histogram.ipynb `
.. only:: html
.. rst-class:: sphx-glr-signature
Keywords: matplotlib code example, codex, python plot, pyplot
`Gallery generated by Sphinx-Gallery
`_