Matplotlib Animation



Examples of animated visualization using Matplotlib.

Matplotlib is probably the most popular data/scientific visualization library for python. Despite normally working with static figures, it also provides some decent ways to produce moving imaging. Let’s take a look in how to export visually our animated data.

Exporting image sequence


To export an image sequence you basically have to loop through a batch of functions which are responsible for saving our Matplotlib’s imshows. After getting the picture sequence (ordered or not), we are able to produce videos, GIFs or any kind of moving visual image media by using editors such as Gimp, Photoshop, After Effects and so one.

Color shifting


For this example, we have a sequence of pictures like image.0000.png, image.0001.pngimage.0008.png, similar to the following example, which shows us the color hue shifting of a given input image.

Exporting GIFs


Before exporting GIFs, we have to install a free and open source software called ImageMagick, which is going to work together with Matplotlib’s animation module. To install ImageMagick on Linux and OSX, run the following command:

$ sudo apt install imagemagick

or

$ conda install --channel conda-forge imagemagick

For windows folow ImageMagick download.

Reaction-Diffusion system


For the following example, we have an animated GIF showing a Reaction Diffusion System. If you want to know more how it works, take a look at this notebook.

Reaction Diffusion

Exporting videos


On the same way of GIFs exportation, we have to install an extra dependency to work together with Matplotlib’s animation module. In this instance, the well-known FFmpeg is maybe our best choice (at least as far as I know). To install this on Linux and OSX, run:

$ sudo apt-get install ffmpeg

or

$ conda install --channel conda-forge ffmpeg

Band-pass filter


For this example, we are going to produce an animated 2D Bandpass Filter. First step is to load an image and get its spectrum.