animation#

class animation(fig=None, filename=None, dpi=200, fps=10, imageformat='png', basename='animation', nametemplate=None, imagefolder=None, anim_args=None, save_args=None, frames=None, tidy=True, verbose=True, **kwargs)[source]#

Bases: prettyobj

A class for storing and saving a Matplotlib animation.

See also sc.savemovie(), which works directly with Matplotlib artists rather than an entire figure. Depending on your use case, one is likely easier to use than the other. Use sc.animation() if you want to animate a complex figure including non-artist objects (e.g., titles and legends); use sc.savemovie() if you just want to animate a set of artists (e.g., lines).

This class works by saving snapshots of the figure to disk as image files, then reloading them either via ffmpeg or as a Matplotlib animation. While (slightly) slower than working with artists directly, it means that anything that can be rendered to a figure can be animated.

Note: the terms “animation” and “movie” are used interchangeably here.

Parameters:
  • fig (fig) – the Matplotlib figure to animate (if none, use current)

  • filename (str) – the name of the output animation (default: animation.mp4)

  • dpi (int) – the resolution to save the animation at

  • fps (int) – frames per second for the animation

  • imageformat (str) – file type for temporary image files, e.g. ‘jpg’

  • basename (str) – name for temporary image files, e.g. ‘myanimation’

  • nametemplate (str) – as an alternative to imageformat and basename, specify the full name template, e.g. ‘myanimation%004d.jpg’

  • imagefolder (str) – location to store temporary image files; default current folder, or use ‘tempfile’ to create a temporary folder

  • anim_args (dict) – passed to matplotlib.animation.ArtistAnimation or ffmpeg.input()

  • save_args (dict) – passed to animation.save() or ffmpeg.run()

  • tidy (bool) – whether to delete temporary files

  • verbose (bool) – whether to print progress

  • kwargs (dict) – also passed to animation.save()

Example:

anim = sc.animation()

plt.figure()
repeats = 21
colors = sc.vectocolor(repeats, cmap='turbo')
for i in range(repeats):
    scale = 1/np.sqrt(i+1)
    x = scale*np.random.randn(10)
    y = scale*np.random.randn(10)
    label = str(i) if not(i%5) else None
    plt.scatter(x, y, c=[colors[i]], label=label)
    plt.title(f'Scale = 1/√{i}')
    plt.legend()
    sc.boxoff('all')
    anim.addframe()

anim.save('dots.mp4')
New in version 1.3.3.
New in version 2.0.0: ffmpeg option.

Attributes

n_files

n_frames

Methods

initialize()[source]#

Handle additional initialization of variables

__add__(*args, **kwargs)[source]#

Allow anim += fig

addframe(fig=None, *args, **kwargs)[source]#

Add a frame to the animation – typically a figure object, but can also be an artist or list of artists

loadframes()[source]#

Load saved images as artists

rmfiles()[source]#

Remove temporary image files

save(filename=None, fps=None, dpi=None, engine='ffmpeg', anim_args=None, save_args=None, frames=None, tidy=None, verbose=True, **kwargs)[source]#

Save the animation – arguments the same as sc.animation() and sc.savemovie(), and are described there