Based on its plotting functionality, Matplotlib also provides an interface to
generate animations using the animation module. An
animation is a sequence of frames where each frame corresponds to a plot on a
Figure. This tutorial covers a general guideline on
how to create such animations and the different options available. More information is available in the API description: animation
import
matplotlib.pyplot
as
plt
import
numpy
as
np
import
matplotlib.animation
as
animation
Animation classes
The animation process in Matplotlib can be thought of in 2 different ways:
FuncAnimation: Generate data for first
frame and then modify this data for each frame to create an animated plot.
ArtistAnimation: Generate a list (iterable)
of artists that will draw in each frame in the animation.
FuncAnimation is more efficient in terms of
speed and memory as it draws an artist once and then modifies it. On the
other hand ArtistAnimation is flexible as it
allows any iterable of artists to be animated in a sequence.
FuncAnimation
The FuncAnimation class allows us to create an
animation by passing a function that iteratively modifies the data of a plot.
This is achieved by using the setter methods on various
Artist (examples: Line2D,
PathCollection, etc.). A usual
FuncAnimation object takes a
that we want to animate and a function
that modifies the data plotted on the figure. It uses the
parameter to determine the length of the animation. The parameter
is used to determine time in milliseconds between drawing of two frames.
Animating using typically requires these steps:
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.
Create an animation function that updates the artists for a given frame.
Typically, this calls set_* methods of the artists.
The following table shows a few plotting methods, the artists they return and some
commonly used set_* methods that update the underlying data. While updating data
is the most common operation in animations, you can also update other aspects such as
color or text position.
Covering the set methods for all types of artists is beyond the scope of this
tutorial but can be found in their respective documentations. An example of
such update methods in use for Axes.scatter and Axes.plot is as follows.
ArtistAnimation can be used
to generate animations if there is data stored on various different artists.
This list of artists is then converted frame by frame into an animation. For
example, when we use Axes.barh to plot a bar-chart, it creates a number of
artists for each of the bar and error bars. To update the plot, one would
need to update each of the bars from the container individually and redraw
them. Instead, animation.ArtistAnimation can be used to plot each frame
individually and then stitched together to form an animation. A barchart race
is a simple example for this.
Animation writers
Animation objects can be saved to disk using various multimedia writers
(ex: Pillow, ffpmeg, imagemagick). Not all video formats are supported
by all writers. There are 4 major types of writers:
PillowWriter - Uses the Pillow library to
create the animation.
HTMLWriter - Used to create JavaScript-based
animations.
Pipe-based writers - FFMpegWriter and
ImageMagickWriter are pipe based writers.
These writers pipe each frame to the utility (ffmpeg / imagemagick)
which then stitches all of them together to create the animation.
File-based writers - FFMpegFileWriter and
ImageMagickFileWriter are examples of
file-based writers. These writers are slower than their pipe-based
alternatives but are more useful for debugging as they save each frame in
a file before stitching them together into an animation.
All formats supported by imagemagick: magick-listformat
To save animations using any of the writers, we can use the
animation.Animation.save method. It takes the filename that we want to
save the animation as and the writer, which is either a string or a writer
object. It also takes an fps argument. This argument is different than the
interval argument that FuncAnimation or
ArtistAnimation uses. fps determines the frame rate that the
saved animation uses, whereas interval determines the frame rate that
the displayed animation uses.
Below are a few examples that show how to save an animation with different
writers.
(the extra_args for apng are needed to reduce filesize by ~10x)
Note that ffmpeg and imagemagick need to be separately installed.
A cross-platform way to obtain ffmpeg is to install the imageio_ffmpeg
PyPI package, and then to set
rcParams["animation.ffmpeg_path"]=imageio_ffmpeg.get_ffmpeg_exe().
Total running time of the script: (0 minutes 16.925 seconds)