From 0070ad81f6c6c22c6a0bf435df28b90ab724c5e4 Mon Sep 17 00:00:00 2001 From: Vamshi Date: Fri, 19 Jan 2024 21:58:56 +0530 Subject: [PATCH] example of animation update function tha takes arguments Co-authored-by: hannah --- .../animation/funcanimation_arguments.py | 68 +++++++++++++++++++ 1 file changed, 68 insertions(+) create mode 100644 galleries/examples/animation/funcanimation_arguments.py diff --git a/galleries/examples/animation/funcanimation_arguments.py b/galleries/examples/animation/funcanimation_arguments.py new file mode 100644 index 000000000000..2b471daf7d21 --- /dev/null +++ b/galleries/examples/animation/funcanimation_arguments.py @@ -0,0 +1,68 @@ +""" +================================== +Parameterized animation function +================================== + + +Create an `.FuncAnimation` animation updating function that takes as input the frame, +objects being animated, and data set. +""" + +import functools + +import matplotlib.pyplot as plt +import numpy as np + +import matplotlib.animation as animation + + +def update(frame, line, text, decades, widgets_data): + # frame (int): The current frame number. + # line (Line2D): The line object to update. + # text (Text): The text annotation object to update. + # decades (numpy.ndarray): Array of decades. + # widgets_data (numpy.ndarray): Array of widgets data. + + current_decade = decades[frame] + current_widgets = int(widgets_data[frame]) + + line.set_data(decades[:frame + 1], widgets_data[:frame + 1]) + text.set_text(f'Decade: {current_decade}\nNumber of Widgets: {current_widgets}') + + return line, text + +# Set up the animation + +# Constants +decades = np.arange(1940, 2020, 10) +initial_widgets = 10000 # Rough estimate of the no. of widgets in the 1950s + +# Generate rough growth data +growth_rate = np.random.uniform(1.02, 3.10, size=len(decades)) +widgets_data = np.cumprod(growth_rate) * initial_widgets + +# Set up the initial plot +fig, ax = plt.subplots() + +# create an empty line +line, = ax.plot([], []) + +# display the current decade +text = ax.text(0.5, 0.85, '', transform=ax.transAxes, + fontsize=12, ha='center', va='center') + +ax.set(xlabel='Decade', ylabel='Number of Widgets', + xlim=(1940, 2020), + ylim=(0, max(widgets_data) + 100000)) + + +ani = animation.FuncAnimation( + fig, + # bind arguments to the update function, leave only frame unbound + functools.partial(update, line=line, text=text, + decades=decades, widgets_data=widgets_data), + frames=len(decades), + interval=1000, +) + +plt.show()