matplotlib.animation.FuncAnimation#

classmatplotlib.animation.FuncAnimation(fig,func,frames=None,init_func=None,fargs=None,save_count=None,*,cache_frame_data=True,**kwargs)[source]#

TimedAnimation subclass that makes an animation by repeatedly callinga functionfunc.

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:
figFigure

The figure object used to get needed events, such as draw or resize.

funccallable

The function to call at each frame. The first argument willbe the next value inframes. Any additional positionalarguments can be supplied usingfunctools.partial or via thefargsparameter.

The required signature is:

deffunc(frame,*fargs)->iterable_of_artists

It is often more convenient to provide the arguments usingfunctools.partial. In this way it is also possible to pass keywordarguments. To pass a function with both positional and keywordarguments, set all arguments as keyword arguments, just leaving theframe argument unset:

deffunc(frame,art,*,y=None):...ani=FuncAnimation(fig,partial(func,art=ln,y='foo'))

Ifblit==True,func must return an iterable of all artiststhat were modified or created. This information is used by the blittingalgorithm to determine which parts of the figure have to be updated.The return value is unused ifblit==False and may be omitted inthat case.

framesiterable, int, generator function, or None, optional

Source of data to passfunc and each frame of the animation

  • If an iterable, then simply use the values provided. If theiterable has a length, it will override thesave_count kwarg.

  • If an integer, then equivalent to passingrange(frames)

  • If a generator function, then must have the signature:

    defgen_function()->obj
  • IfNone, then equivalent to passingitertools.count.

In all of these cases, the values inframes is simply passed throughto the user-suppliedfunc and thus can be of any type.

init_funccallable, optional

A function used to draw a clear frame. If not given, the results ofdrawing from the first item in the frames sequence will be used. Thisfunction will be called once before the first frame.

The required signature is:

definit_func()->iterable_of_artists

Ifblit==True,init_func must return an iterable of artiststo be re-drawn. This information is used by the blitting algorithm todetermine which parts of the figure have to be updated. The returnvalue is unused ifblit==False and may be omitted in that case.

fargstuple or None, optional

Additional arguments to pass to each call tofunc. Note: the use offunctools.partial is preferred overfargs. Seefunc for details.

save_countint, optional

Fallback for the number of values fromframes to cache. This isonly used if the number of frames cannot be inferred fromframes,i.e. when it's an iterator without length or a generator.

intervalint, default: 200

Delay between frames in milliseconds.

repeat_delayint, default: 0

The delay in milliseconds between consecutive animation runs, ifrepeat is True.

repeatbool, default: True

Whether the animation repeats when the sequence of frames is completed.

blitbool, default: False

Whether blitting is used to optimize drawing. Note: when usingblitting, any animated artists will be drawn according to their zorder;however, they will be drawn on top of any previous artists, regardlessof their zorder.

cache_frame_databool, default: True

Whether frame data is cached. Disabling cache might be helpful whenframes contain large objects.

__init__(fig,func,frames=None,init_func=None,fargs=None,save_count=None,*,cache_frame_data=True,**kwargs)[source]#

Methods

__init__(fig, func[, frames, init_func, ...])

new_frame_seq()

Return a new sequence of frame information.

new_saved_frame_seq()

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.

new_frame_seq()[source]#

Return a new sequence of frame information.

new_saved_frame_seq()[source]#

Return a new sequence of saved/cached frame information.