matplotlib.animation.Animation#
- classmatplotlib.animation.Animation(fig,event_source=None,blit=False)[source]#
A base class for Animations.
This class is not usable as is, and should be subclassed to provide neededbehavior.
Note
You must store the created Animation in a variable that lives as longas the animation should run. Otherwise, the Animation object will begarbage-collected and the animation stops.
- Parameters:
- fig
Figure The figure object used to get needed events, such as draw or resize.
- event_sourceobject, optional
A class that can run a callback when desired eventsare generated, as well as be stopped and started.
Examples include timers (see
TimedAnimation) and filesystem notifications.- blitbool, default: False
Whether blitting is used to optimize drawing. If the backend does notsupport blitting, then this parameter has no effect.
- fig
See also
Methods
__init__(fig[, event_source, blit])Return a new sequence of frame information.
Return a new sequence of saved/cached frame information.
pause()Pause the animation.
resume()Resume the animation.
save(filename[, writer, fps, dpi, codec, ...])Save the animation as a movie file by drawing every frame.
to_html5_video([embed_limit])Convert the animation to an HTML5
<video>tag.to_jshtml([fps, embed_frames, default_mode])Generate HTML representation of the animation.
- save(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.
- Parameters:
- filenamestr
The output filename, e.g.,
mymovie.mp4.- writer
MovieWriteror str, default:rcParams["animation.writer"](default:'ffmpeg') A
MovieWriterinstance to use or a key that identifies aclass to use, such as 'ffmpeg'.- fpsint, optional
Movie frame rate (per second). If not set, the frame rate from theanimation's frame interval.
- dpifloat, default:
rcParams["savefig.dpi"](default:'figure') Controls the dots per inch for the movie frames. Together withthe figure's size in inches, this controls the size of the movie.
- codecstr, default:
rcParams["animation.codec"](default:'h264'). The video codec to use. Not all codecs are supported by a given
MovieWriter.- bitrateint, default:
rcParams["animation.bitrate"](default:-1) The bitrate of the movie, in kilobits per second. Higher valuesmeans higher quality movies, but increase the file size. A valueof -1 lets the underlying movie encoder select the bitrate.
- extra_argslist of str or None, optional
Extra command-line arguments passed to the underlying movie encoder. Thesearguments are passed last to the encoder, just before the output filename.The default, None, means to use
rcParams["animation.[name-of-encoder]_args"]forthe builtin writers.- metadatadict[str, str], default: {}
Dictionary of keys and values for metadata to include inthe output file. Some keys that may be of use include:title, artist, genre, subject, copyright, srcform, comment.
- extra_animlist, default: []
Additional
Animationobjects that should be includedin the saved movie file. These need to be from the sameFigureinstance. Also, animation frames willjust be simply combined, so there should be a 1:1 correspondencebetween the frames from the different animations.- savefig_kwargsdict, default: {}
Keyword arguments passed to each
savefigcall used tosave the individual frames.- progress_callbackfunction, optional
A callback function that will be called for every frame to notifythe saving progress. It must have the signature
deffunc(current_frame:int,total_frames:int)->Any
wherecurrent_frame is the current frame number andtotal_frames is thetotal number of frames to be saved.total_frames is set to None, if thetotal number of frames cannot be determined. Return values may exist but areignored.
Example code to write the progress to stdout:
progress_callback=lambdai,n:print(f'Saving frame{i}/{n}')
Notes
fps,codec,bitrate,extra_args andmetadata are used toconstruct a
MovieWriterinstance and can only be passed ifwriter is a string. If they are passed as non-None andwriteris aMovieWriter, aRuntimeErrorwill be raised.
- to_html5_video(embed_limit=None)[source]#
Convert the animation to an HTML5
<video>tag.This saves the animation as an h264 video, encoded in base64directly into the HTML5 video tag. This respects
rcParams["animation.writer"](default:'ffmpeg')andrcParams["animation.bitrate"](default:-1). This also makes use of theinterval to control the speed, and uses therepeatparameter to decide whether to loop.- Parameters:
- embed_limitfloat, optional
Limit, in MB, of the returned animation. No animation is createdif the limit is exceeded.Defaults to
rcParams["animation.embed_limit"](default:20.0) = 20.0.
- Returns:
- str
An HTML5 video tag with the animation embedded as base64 encodedh264 video.If theembed_limit is exceeded, this returns the string"Video too large to embed."
- to_jshtml(fps=None,embed_frames=True,default_mode=None)[source]#
Generate HTML representation of the animation.
- Parameters:
- fpsint, optional
Movie frame rate (per second). If not set, the frame rate fromthe animation's frame interval.
- embed_framesbool, optional
- default_modestr, optional
What to do when the animation ends. Must be one of
{'loop','once','reflect'}. Defaults to'loop'if therepeatparameter is True, otherwise'once'.
- Returns:
- str
An HTML representation of the animation embedded as a js object asproduced with the
HTMLWriter.