class matplotlib.animation.Animation(fig, event_source=None, blit=False)[source]

This class wraps the creation of an animation using matplotlib.

It is only a base class which should be subclassed to provide needed behavior.

This class is not typically used directly.


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

event_sourceobject, optional

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.

blitbool, optional

controls whether blitting is used to optimize drawing. Defaults to False.

__init__(self, fig, event_source=None, blit=False)[source]

Initialize self. See help(type(self)) for accurate signature.


__init__(self, fig[, event_source, blit]) Initialize self.
new_frame_seq(self) Return a new sequence of frame information.
new_saved_frame_seq(self) Return a new sequence of saved/cached frame information.
save(self, filename[, writer, fps, dpi, ...]) Save the animation as a movie file by drawing every frame.
to_html5_video(self[, embed_limit]) Convert the animation to an HTML5 <video> tag.
to_jshtml(self[, fps, embed_frames, ...]) Generate HTML representation of the animation

Return a new sequence of frame information.


Return a new sequence of saved/cached frame information.

save(self, filename, writer=None, fps=None, dpi=None, codec=None, bitrate=None, extra_args=None, metadata=None, extra_anim=None, savefig_kwargs=None, \*, progress_callback=None)[source]

Save the animation as a movie file by drawing every frame.


The output filename, e.g., mymovie.mp4.

writerMovieWriter or str, optional

A MovieWriter instance to use or a key that identifies a class to use, such as 'ffmpeg'. If None, defaults to rcParams["animation.writer"] (default: 'ffmpeg') = 'ffmpeg'.

fpsnumber, optional

Frames per second in the movie. Defaults to None, which will use the animation's specified interval to set the frames per second.

dpinumber, optional

Controls the dots per inch for the movie frames. This combined with the figure's size in inches controls the size of the movie. If None, defaults to rcParams["savefig.dpi"] (default: 'figure').

codecstr, optional

The video codec to be used. Not all codecs are supported by a given MovieWriter. If None, default to rcParams["animation.codec"] (default: 'h264') = 'h264'.

bitratenumber, optional

Specifies the number 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 None, defaults to rcParams["animation.bitrate"] (default: -1) = -1.

extra_argslist, optional

List of extra string arguments to be passed to the underlying movie utility. If None, defaults to rcParams["animation.extra_args"].

metadataDict[str, str], optional

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_animlist, optional

Additional Animation objects that should be included in the saved movie file. These need to be from the same matplotlib.figure.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_kwargsdict, optional

Is a dictionary containing keyword arguments to be passed on to the savefig command which is called repeatedly to save the individual frames.

progress_callbackfunction, optional

A callback function that will be called for every frame to notify the saving progress. It must have the signature

def func(current_frame: int, total_frames: int) -> Any

where current_frame is the current frame number and total_frames is the total number of frames to be saved. total_frames is set to None, if the total number of frames can not be determined. Return values may exist but are ignored.

Example code to write the progress to stdout:

progress_callback =                    lambda i, n: print(f'Saving frame {i} of {n}')


fps, codec, bitrate, extra_args and metadata are used to construct a MovieWriter instance and can only be passed if writer is a string. If they are passed as non-None and writer is a MovieWriter, a RuntimeError will be raised.

to_html5_video(self, embed_limit=None)[source]

Convert the animation to an HTML5 <video> tag.

This saves the animation as an h264 video, encoded in base64 directly into the HTML5 video tag. This respects the rc parameters for the writer as well as the bitrate. This also makes use of the interval to control the speed, and uses the repeat parameter to decide whether to loop.

embed_limitfloat, optional

Limit, in MB, of the returned animation. No animation is created if the limit is exceeded. Defaults to rcParams["animation.embed_limit"] (default: 20.0) = 20.0.


An HTML5 video tag with the animation embedded as base64 encoded h264 video. If the embed_limit is exceeded, this returns the string "Video too large to embed."

to_jshtml(self, fps=None, embed_frames=True, default_mode=None)[source]

Generate HTML representation of the animation