# Data Animations With Python and MoviePy

Python has some great data visualization librairies, but few can render GIFs or video animations. This post shows how to use MoviePy as a generic animation plugin for any other library.

MoviePy lets you define custom animations with a function make_frame(t), which returns the video frame corresponding to time t (in seconds):

In previous posts I used this method to animate vector graphics (with the library Gizeh), and ray-traced 3D scenes (generated by POV-Ray). This post covers the scientific libraries Mayavi, Vispy, Matplotlib, Numpy, and Scikit-image.

## Animations with Mayavi

Mayavi is a Python module for interactive 3D data visualization with a simple interface. In this first example we animate a surface whose elevation depends on the time t:

Another example with a wireframe mesh whose coordinates and view angle depend on the time :

As Mayavi relies on the powerful ITK visualization engine it can also process complex datasets. Here is an animation derived from a Mayavi example:

## Animations with Vispy

Vispy is another interactive 3D data visualization library, based on OpenGL. As for Mayavi, we first create a figure and a mesh, that we animate with MoviePy.

Here are more advanced examples (derived from the Vispy gallery) where C code snippets are embedded in the Python code to fine-tune the 3D shaders:

## Animations with Matplotlib

The 2D/3D plotting library Matplotlib already has an animation module, but I found that MoviePy produces lighter, better quality videos, while being up to two times faster (not sure why, see here for more details). Here is how you animate Matplotlib with MoviePy:

Matplotlib has many beautiful themes and works well with numerical modules like Pandas or Scikit-Learn. Let us watch a SVM classifier getting a better understanding of the map as the number of training point increases.

Put simply, the background colors tell us where the classifier thinks the black points and white points belong. At the begining it has no real clue, but as more points appear it progressively understands that they are distributed along moon-shaped regions.

## Animations with Numpy

If you are working with Numpy arrays (Numpy is the central numerical library in Python), you don’t need any external plotting library, you can feed the arrays directly to MoviePy.

This is well illustrated by this simulation of a zombie outbreak in France (inspired by this blog post by Max Berggren). France is modelled as a grid (Numpy array) on which all the computations for dispersion and infection are done. At regular intervals, a few Numpy operations tranform the grid into a valid RGB image, and send it to MoviePy.

## Putting animations together

What is better than an animation ? Two animations ! You can take advantage of MoviePy’s video composition capabilities to mix animations from different libraries:

Or for something more artistic:

It may be a tad too flashy, but sometimes you must give your audience something they can tweet.

You can also annotate the animations, which is useful when comparing different filters or algorithms. Let’s display four image transformations from the library Scikit-image:

If we replace CompositeVideoClip and clips_array by concatenate_videoclips we get a title-effect type animation:

Finally, MoviePy will be particularly practical when dealing with video data, as it is its first job. For our last example we estimate the size of a growing bacterial population by thresholding the video frames and counting the white pixels. The third panel shows that the population size grows exponentially in time.

## One library to animate them all ?

I hope to have given you enough recipes to impress your colleagues at your next presentation. Any other library could be animated with MoviePy, as long as its output can be converted to a Numpy array.

Some libraries have their own animation modules, but these are usually a pain to fix and maintain. Thanks to the many users who have tested it in very different contexts, MoviePy seems to have become stable (or people stopped reporting bugs), and can be adapted to many situations. There is still a lot to do, but it would be nice if authors started relying on it for video and GIF rendering, like Pandas and Scikit-Learn rely on Matplotlib for plotting.

For completeness, and because it may better fit your needs, I must mention ImageIO, another Python library with video writing capabilities which focuses on providing a very simple interface to read or write any kind of image, video or volumetric data. For instance you use imwrite() to write any image, mimwrite() for any video/GIF, volwrite() for volumetric data, or simply write() for streamed data.

Cheers, and happy GIFing !