From db252260f0f8eca0a52e91dce0738109bf847757 Mon Sep 17 00:00:00 2001 From: Tim Hoffmann <2836374+timhoffm@users.noreply.github.com> Date: Wed, 6 Mar 2024 00:33:55 +0100 Subject: [PATCH] DOC: Update some animation related topics - use `set_data_3d` - cross-reference `Line2d` `set_data`, `set_xdata`, `set_ydata` - Rewrite parts of the FuncAnimation description Inspired through #27830. --- galleries/examples/animation/random_walk.py | 4 +- .../users_explain/animations/animations.py | 44 +++++++++++-------- lib/matplotlib/lines.py | 15 +++++++ 3 files changed, 41 insertions(+), 22 deletions(-) diff --git a/galleries/examples/animation/random_walk.py b/galleries/examples/animation/random_walk.py index 10dcf5ade164..4be0b461f933 100644 --- a/galleries/examples/animation/random_walk.py +++ b/galleries/examples/animation/random_walk.py @@ -25,9 +25,7 @@ def random_walk(num_steps, max_step=0.05): def update_lines(num, walks, lines): for line, walk in zip(lines, walks): - # NOTE: there is no .set_data() for 3 dim data... - line.set_data(walk[:num, :2].T) - line.set_3d_properties(walk[:num, 2]) + line.set_data_3d(walk[:num, :].T) return lines diff --git a/galleries/users_explain/animations/animations.py b/galleries/users_explain/animations/animations.py index fb8564f8318e..0391d9bc030a 100644 --- a/galleries/users_explain/animations/animations.py +++ b/galleries/users_explain/animations/animations.py @@ -49,28 +49,33 @@ # *func* that modifies the data plotted on the figure. It uses the *frames* # parameter to determine the length of the animation. The *interval* parameter # is used to determine time in milliseconds between drawing of two frames. -# Animating using `.FuncAnimation` would usually follow the following -# structure: -# -# - Plot the initial figure, including all the required artists. Save all the -# artists in variables so that they can be updated later on during the -# animation. -# - Create an animation function that updates the data in each artist to -# generate the new frame at each function call. -# - Create a `.FuncAnimation` object with the `.Figure` and the animation -# function, along with the keyword arguments that determine the animation -# properties. -# - Use `.animation.Animation.save` or `.pyplot.show` to save or show the -# animation. -# -# The update function uses the ``set_*`` function for different artists to -# modify the data. The following table shows a few plotting methods, the artist -# types they return and some methods that can be used to update them. +# Animating using `.FuncAnimation` typically requires these steps: +# +# 1) Plot the initial figure as you would in a static plot. Save all the created +# artists, which are returned by the plot functions, in variables so that you can +# access and modify them later in the animation function. +# 2) Create an animation function that updates the artists for a given frame. +# Typically, this calls ``set_*`` methods of the artists. +# 3) Create a `.FuncAnimation`, passing the `.Figure` and the animation function. +# 4) Save or show the animation using one of the following methods: +# +# - `.pyplot.show` to show the animation in a window +# - `.Animation.to_html5_video` to create a HTML ``