Writing Matplotlib plot/movie sequences with lossy .avi/.mp4

Matplotlib on any platform can use Ffmpeg, Avconv or Mencoder to directly write lossy compressed and lossless compressed movies created from sequences of plots.

Why create movies from Matplotlib plots

Instead of creating hundreds of PNGs, or skipping plots and missing details, creating movies of a large sequence of plots is highly effective for many processes that evolve across time and/or space.

You can alternatively write a lot of PNGs and then use external programs to convert to AVI, but it’s simpler and often faster and more robust to do this from Matplotlib without creating all the .png files.

Matplotlib movie writing quality

The default “auto” bitrate makes excessively compressed, blocky movies. Override the default auto-bitrate with the following snippet:

#!/usr/bin/env python

import matplotlib.animation as anim

Writer = anim.writers['ffmpeg']
writer = Writer(fps=15, codec='mpeg4', bitrate=1e6)
with writer.saving(fg, fn,100):

Leave a Comment