You are reading an old version of the documentation (v1.4.1). For the latest version see https://matplotlib.org/stable/api/animation_api.html
matplotlib

Table Of Contents

Previous topic

afm (Adobe Font Metrics interface)

Next topic

artists

This Page

animation

matplotlib.animation

class matplotlib.animation.AVConvBase

Bases: matplotlib.animation.FFMpegBase

args_key = u'animation.avconv_args'
exec_key = u'animation.avconv_path'
class matplotlib.animation.AVConvFileWriter(*args, **kwargs)

Bases: matplotlib.animation.AVConvBase, matplotlib.animation.FFMpegFileWriter

class matplotlib.animation.AVConvWriter(fps=5, codec=None, bitrate=None, extra_args=None, metadata=None)

Bases: matplotlib.animation.AVConvBase, matplotlib.animation.FFMpegWriter

Construct a new MovieWriter object.

fps: int
Framerate for movie.
codec: string or None, optional
The codec to use. If None (the default) the setting in the rcParam animation.codec is used.
bitrate: int or None, optional
The bitrate for the saved movie file, which is one way to control the output file size and quality. The default value is None, which uses the value stored in the rcParam animation.bitrate. A value of -1 implies that the bitrate should be determined automatically by the underlying utility.
extra_args: list of strings or None
A list of extra string arguments to be passed to the underlying movie utiltiy. The default is None, which passes the additional argurments in the ‘animation.extra_args’ rcParam.
metadata: dict of string:string or None
A dictionary of keys and values for metadata to include in the output file. Some keys that may be of use include: title, artist, genre, subject, copyright, srcform, comment.
class matplotlib.animation.Animation(fig, event_source=None, blit=False)

Bases: object

This class wraps the creation of an animation using matplotlib. It is only a base class which should be subclassed to provide needed behavior.

fig is the figure object that is used to get draw, resize, and any other needed events.

event_source is a class that can run a callback when desired events are generated, as well as be stopped and started. Examples include timers (see TimedAnimation) and file system notifications.

blit is a boolean that controls whether blitting is used to optimize drawing.

new_frame_seq()

Creates a new sequence of frame information.

new_saved_frame_seq()

Creates a new sequence of saved/cached frame information.

save(filename, writer=None, fps=None, dpi=None, codec=None, bitrate=None, extra_args=None, metadata=None, extra_anim=None, savefig_kwargs=None)

Saves a movie file by drawing every frame.

filename is the output filename, e.g., mymovie.mp4

writer is either an instance of MovieWriter or a string key that identifies a class to use, such as ‘ffmpeg’ or ‘mencoder’. If nothing is passed, the value of the rcparam animation.writer is used.

fps is the frames per second in the movie. Defaults to None, which will use the animation’s specified interval to set the frames per second.

dpi controls the dots per inch for the movie frames. This combined with the figure’s size in inches controls the size of the movie.

codec is the video codec to be used. Not all codecs are supported by a given MovieWriter. If none is given, this defaults to the value specified by the rcparam animation.codec.

bitrate specifies the amount of bits used per second in the compressed movie, in kilobits per second. A higher number means a higher quality movie, but at the cost of increased file size. If no value is given, this defaults to the value given by the rcparam animation.bitrate.

extra_args is a list of extra string arguments to be passed to the underlying movie utiltiy. The default is None, which passes the additional argurments in the ‘animation.extra_args’ rcParam.

metadata is a dictionary of keys and values for metadata to include in the output file. Some keys that may be of use include: title, artist, genre, subject, copyright, srcform, comment.

extra_anim is a list of additional Animation objects that should be included in the saved movie file. These need to be from the same matplotlib.Figure instance. Also, animation frames will just be simply combined, so there should be a 1:1 correspondence between the frames from the different animations.

savefig_kwargs is a dictionary containing keyword arguments to be passed on to the ‘savefig’ command which is called repeatedly to save the individual frames. This can be used to set tight bounding boxes, for example.

class matplotlib.animation.ArtistAnimation(fig, artists, *args, **kwargs)

Bases: matplotlib.animation.TimedAnimation

Before calling this function, all plotting should have taken place and the relevant artists saved.

frame_info is a list, with each list entry a collection of artists that represent what needs to be enabled on each frame. These will be disabled for other frames.

class matplotlib.animation.FFMpegBase
args_key = u'animation.ffmpeg_args'
exec_key = u'animation.ffmpeg_path'
output_args
class matplotlib.animation.FFMpegFileWriter(*args, **kwargs)

Bases: matplotlib.animation.FileMovieWriter, matplotlib.animation.FFMpegBase

supported_formats = [u'png', u'jpeg', u'ppm', u'tiff', u'sgi', u'bmp', u'pbm', u'raw', u'rgba']
class matplotlib.animation.FFMpegWriter(fps=5, codec=None, bitrate=None, extra_args=None, metadata=None)

Bases: matplotlib.animation.MovieWriter, matplotlib.animation.FFMpegBase

Construct a new MovieWriter object.

fps: int
Framerate for movie.
codec: string or None, optional
The codec to use. If None (the default) the setting in the rcParam animation.codec is used.
bitrate: int or None, optional
The bitrate for the saved movie file, which is one way to control the output file size and quality. The default value is None, which uses the value stored in the rcParam animation.bitrate. A value of -1 implies that the bitrate should be determined automatically by the underlying utility.
extra_args: list of strings or None
A list of extra string arguments to be passed to the underlying movie utiltiy. The default is None, which passes the additional argurments in the ‘animation.extra_args’ rcParam.
metadata: dict of string:string or None
A dictionary of keys and values for metadata to include in the output file. Some keys that may be of use include: title, artist, genre, subject, copyright, srcform, comment.
class matplotlib.animation.FileMovieWriter(*args, **kwargs)

Bases: matplotlib.animation.MovieWriter

MovieWriter subclass that handles writing to a file.

cleanup()
finish()
frame_format

Format (png, jpeg, etc.) to use for saving the frames, which can be decided by the individual subclasses.

grab_frame(**savefig_kwargs)

Grab the image information from the figure and save as a movie frame. All keyword arguments in savefig_kwargs are passed on to the ‘savefig’ command that saves the figure.

setup(fig, outfile, dpi, frame_prefix=u'_tmp', clear_temp=True)

Perform setup for writing the movie file.

fig: matplotlib.Figure instance
The figure object that contains the information for frames
outfile: string
The filename of the resulting movie file
dpi: int
The DPI (or resolution) for the file. This controls the size in pixels of the resulting movie file.
frame_prefix: string, optional
The filename prefix to use for the temporary files. Defaults to ‘_tmp’
clear_temp: bool
Specifies whether the temporary files should be deleted after the movie is written. (Useful for debugging.) Defaults to True.
class matplotlib.animation.FuncAnimation(fig, func, frames=None, init_func=None, fargs=None, save_count=None, **kwargs)

Bases: matplotlib.animation.TimedAnimation

Makes an animation by repeatedly calling a function func, passing in (optional) arguments in fargs.

frames can be a generator, an iterable, or a number of frames.

init_func is a function used to draw a clear frame. If not given, the results of drawing from the first item in the frames sequence will be used. This function will be called once before the first frame.

If blit=True, func and init_func should return an iterable of drawables to clear.

kwargs include repeat, repeat_delay, and interval: interval draws a new frame every interval milliseconds. repeat controls whether the animation should repeat when the sequence of frames is completed. repeat_delay optionally adds a delay in milliseconds before repeating the animation.

new_frame_seq()
new_saved_frame_seq()
class matplotlib.animation.ImageMagickBase
args_key = u'animation.convert_args'
delay
exec_key = u'animation.convert_path'
output_args
class matplotlib.animation.ImageMagickFileWriter(*args, **kwargs)

Bases: matplotlib.animation.FileMovieWriter, matplotlib.animation.ImageMagickBase

supported_formats = [u'png', u'jpeg', u'ppm', u'tiff', u'sgi', u'bmp', u'pbm', u'raw', u'rgba']
class matplotlib.animation.ImageMagickWriter(fps=5, codec=None, bitrate=None, extra_args=None, metadata=None)

Bases: matplotlib.animation.MovieWriter, matplotlib.animation.ImageMagickBase

Construct a new MovieWriter object.

fps: int
Framerate for movie.
codec: string or None, optional
The codec to use. If None (the default) the setting in the rcParam animation.codec is used.
bitrate: int or None, optional
The bitrate for the saved movie file, which is one way to control the output file size and quality. The default value is None, which uses the value stored in the rcParam animation.bitrate. A value of -1 implies that the bitrate should be determined automatically by the underlying utility.
extra_args: list of strings or None
A list of extra string arguments to be passed to the underlying movie utiltiy. The default is None, which passes the additional argurments in the ‘animation.extra_args’ rcParam.
metadata: dict of string:string or None
A dictionary of keys and values for metadata to include in the output file. Some keys that may be of use include: title, artist, genre, subject, copyright, srcform, comment.
class matplotlib.animation.MencoderBase
allowed_metadata = [u'name', u'artist', u'genre', u'subject', u'copyright', u'srcform', u'comment']
args_key = u'animation.mencoder_args'
exec_key = u'animation.mencoder_path'
output_args
class matplotlib.animation.MencoderFileWriter(*args, **kwargs)

Bases: matplotlib.animation.FileMovieWriter, matplotlib.animation.MencoderBase

supported_formats = [u'png', u'jpeg', u'tga', u'sgi']
class matplotlib.animation.MencoderWriter(fps=5, codec=None, bitrate=None, extra_args=None, metadata=None)

Bases: matplotlib.animation.MovieWriter, matplotlib.animation.MencoderBase

Construct a new MovieWriter object.

fps: int
Framerate for movie.
codec: string or None, optional
The codec to use. If None (the default) the setting in the rcParam animation.codec is used.
bitrate: int or None, optional
The bitrate for the saved movie file, which is one way to control the output file size and quality. The default value is None, which uses the value stored in the rcParam animation.bitrate. A value of -1 implies that the bitrate should be determined automatically by the underlying utility.
extra_args: list of strings or None
A list of extra string arguments to be passed to the underlying movie utiltiy. The default is None, which passes the additional argurments in the ‘animation.extra_args’ rcParam.
metadata: dict of string:string or None
A dictionary of keys and values for metadata to include in the output file. Some keys that may be of use include: title, artist, genre, subject, copyright, srcform, comment.
class matplotlib.animation.MovieWriter(fps=5, codec=None, bitrate=None, extra_args=None, metadata=None)

Bases: object

Base class for writing movies. Fundamentally, what a MovieWriter does is provide is a way to grab frames by calling grab_frame(). setup() is called to start the process and finish() is called afterwards. This class is set up to provide for writing movie frame data to a pipe. saving() is provided as a context manager to facilitate this process as:

with moviewriter.saving('myfile.mp4'):
    # Iterate over frames
    moviewriter.grab_frame()

The use of the context manager ensures that setup and cleanup are performed as necessary.

frame_format: string
The format used in writing frame data, defaults to ‘rgba’

Construct a new MovieWriter object.

fps: int
Framerate for movie.
codec: string or None, optional
The codec to use. If None (the default) the setting in the rcParam animation.codec is used.
bitrate: int or None, optional
The bitrate for the saved movie file, which is one way to control the output file size and quality. The default value is None, which uses the value stored in the rcParam animation.bitrate. A value of -1 implies that the bitrate should be determined automatically by the underlying utility.
extra_args: list of strings or None
A list of extra string arguments to be passed to the underlying movie utiltiy. The default is None, which passes the additional argurments in the ‘animation.extra_args’ rcParam.
metadata: dict of string:string or None
A dictionary of keys and values for metadata to include in the output file. Some keys that may be of use include: title, artist, genre, subject, copyright, srcform, comment.
classmethod bin_path()

Returns the binary path to the commandline tool used by a specific subclass. This is a class method so that the tool can be looked for before making a particular MovieWriter subclass available.

cleanup()

Clean-up and collect the process used to write the movie file.

finish()

Finish any processing for writing the movie.

frame_size

A tuple (width,height) in pixels of a movie frame.

grab_frame(**savefig_kwargs)

Grab the image information from the figure and save as a movie frame. All keyword arguments in savefig_kwargs are passed on to the ‘savefig’ command that saves the figure.

classmethod isAvailable()

Check to see if a MovieWriter subclass is actually available by running the commandline tool.

saving(*args, **kwds)

Context manager to facilitate writing the movie file.

*args are any parameters that should be passed to setup.

setup(fig, outfile, dpi, *args)

Perform setup for writing the movie file.

fig: matplotlib.Figure instance
The figure object that contains the information for frames
outfile: string
The filename of the resulting movie file
dpi: int
The DPI (or resolution) for the file. This controls the size in pixels of the resulting movie file.
class matplotlib.animation.MovieWriterRegistry

Bases: object

is_available(name)
list()

Get a list of available MovieWriters.

register(name)
class matplotlib.animation.TimedAnimation(fig, interval=200, repeat_delay=None, repeat=True, event_source=None, *args, **kwargs)

Bases: matplotlib.animation.Animation

Animation subclass that supports time-based animation, drawing a new frame every interval milliseconds.

repeat controls whether the animation should repeat when the sequence of frames is completed.

repeat_delay optionally adds a delay in milliseconds before repeating the animation.