FuncAnimation#

class FuncAnimation(fig, func, frames=None, init_func=None, fargs=None, save_count=None, *, blit=True, **kwargs)[source]#

Bases: _FastSaveMixin, FuncAnimation

A faster drop-in replacement for matplotlib.animation.FuncAnimation.

The signature matches Matplotlib’s, with two differences: blit defaults to True instead of False, and freeze_layout is added. Saving renders frames directly into the Agg buffer instead of calling savefig once per frame, which removes the per-frame PNG round-trip and the repeated UltraPlot tight-layout pass.

Parameters:
  • fig (Figure) – The figure to animate.

  • func (callable()) – The update function, called as func(frame, *fargs). It should return an iterable of the artists it modified. This is required when blit is True, and lets the fast path skip untouched artists.

  • frames (int, iterable, generator, or None, optional) – Source of frame data, as in Matplotlib.

  • init_func (callable(), optional) – Function drawing the clear frame. Should return the animated artists.

  • fargs (tuple, optional) – Extra positional arguments for func and init_func.

  • save_count (int, optional) – Number of frames to cache from a generator.

  • blit (bool, default: True) – Whether to redraw only the artists returned by func. This is the main source of the speedup, but it means changes to artists that are not returned, such as titles or tick labels, will not show up. Pass False to redraw the whole figure each frame, which is still faster than Matplotlib because the layout solver is frozen.

  • cache_frame_data (bool, default: True) – Whether to cache frame data, as in Matplotlib.

  • **kwargs – Passed to matplotlib.animation.TimedAnimation, e.g. interval, repeat, and repeat_delay.

Examples

>>> import ultraplot as uplt
>>> import numpy as np
>>> fig, ax = uplt.subplots()
>>> x = np.linspace(0, 2 * np.pi, 200)
>>> (line,) = ax.plot(x, np.sin(x))
>>> def update(frame):
...     line.set_ydata(np.sin(x + frame / 10))
...     return (line,)
...
>>> ani = uplt.FuncAnimation(fig, update, frames=100)
>>> ani.save('waves.mp4')