Write Matplotlib movie sequence with .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 stack of PNGs and convert to AVI. But, it’s simpler and often faster and more robust to do this from Matplotlib without creating all the .png files.


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

import matplotlib.animation as anim

Writer = anim.writers['ffmpeg']
writer = Writer(fps=15, codec='mpeg4', bitrate=1e6)
with writer.saving(fg, fn,100):
    # code to plot/update figure