From 1b9b6dc3cd96708d68574983bebde399a27b7a1c Mon Sep 17 00:00:00 2001 From: Ruth Comer Date: Tue, 9 Jan 2024 15:17:06 +0000 Subject: [PATCH 1/2] DOC: simplify histogram animation example --- .../examples/animation/animated_histogram.py | 39 ++++++++----------- 1 file changed, 17 insertions(+), 22 deletions(-) diff --git a/galleries/examples/animation/animated_histogram.py b/galleries/examples/animation/animated_histogram.py index a5055f39c4e5..665dcffae0ac 100644 --- a/galleries/examples/animation/animated_histogram.py +++ b/galleries/examples/animation/animated_histogram.py @@ -12,38 +12,34 @@ import matplotlib.animation as animation -# Fixing random state for reproducibility -np.random.seed(19680801) -# Fixing bin edges +# 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 = np.random.randn(1000) +# Histogram our data with numpy. +data = rng.standard_normal(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. +# a random set of numbers and updates the heights of rectangles. The ``animate`` +# function updates the `.Rectangle` patches on a global instance of +# `.BarContainer` (which in this case is defined below). -def prepare_animation(bar_container): +def animate(frame_number): + # 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) - 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 + return bar_container.patches # %% # 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`. +# `.BarContainer`, which is a collection of `.Rectangle` instances. # Output generated via `matplotlib.animation.Animation.to_jshtml`. @@ -52,8 +48,7 @@ def animate(frame_number): 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) +ani = animation.FuncAnimation(fig, animate, 50, repeat=False, blit=True) plt.show() # %% From 0ce2f2b07d6c41ba2da1f8d5fa069e8286372cf4 Mon Sep 17 00:00:00 2001 From: Ruth Comer Date: Tue, 9 Jan 2024 18:03:50 +0000 Subject: [PATCH 2/2] DOC: demonstrate use of partial in FuncAnimation --- galleries/examples/animation/animated_histogram.py | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/galleries/examples/animation/animated_histogram.py b/galleries/examples/animation/animated_histogram.py index 665dcffae0ac..b040cc3c3442 100644 --- a/galleries/examples/animation/animated_histogram.py +++ b/galleries/examples/animation/animated_histogram.py @@ -7,6 +7,8 @@ histogram. """ +import functools + import matplotlib.pyplot as plt import numpy as np @@ -24,11 +26,10 @@ # %% # 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 a global instance of -# `.BarContainer` (which in this case is defined below). +# function updates the `.Rectangle` patches on an instance of `.BarContainer`. -def animate(frame_number): +def animate(frame_number, bar_container): # Simulate new data coming in. data = rng.standard_normal(1000) n, _ = np.histogram(data, HIST_BINS) @@ -39,7 +40,9 @@ def animate(frame_number): # %% # Using :func:`~matplotlib.pyplot.hist` allows us to get an instance of -# `.BarContainer`, which is a collection of `.Rectangle` instances. +# `.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. # Output generated via `matplotlib.animation.Animation.to_jshtml`. @@ -48,7 +51,8 @@ def animate(frame_number): 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, animate, 50, repeat=False, blit=True) +anim = functools.partial(animate, bar_container=bar_container) +ani = animation.FuncAnimation(fig, anim, 50, repeat=False, blit=True) plt.show() # %%