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

Many times we are plotting a time series of line plots or images using Matplotlib and want to share them easily with colleagues via Dropbox or email. Matplotlib docs don’t make it simple to figure out how to do this. There are multiple ways to write lossless movies from Matplotlib

For much smaller file size at the cost of lower quality video, you can also write lossy compressed matplotlib video

In the example matplotlib_avi.py, just four added lines of code do the AVI writing.

  1. first line tells Matplotlib to use FFMPEG, which is already installed on most Linux systems, and easily installed on Windows and Mac brew install ffmpeg
  2. second line tells Matplotlib to make a lossless FFV1 video at 15 frames/sec. (you can optionally use mpeg4 lossy, but I think you should use lossless to help avoid washing out details of plots).
  3. third line says to use 100 DPI (smaller DPI–smaller file and movie size)

    Writer = anim.writers[‘ffmpeg’] writer = Writer(fps=15, codec=’ffv1’) … with writer.saving(fg, fn,100): … writer.grab_frame(facecolor=’k’)

Minimum AVI frame rate

Note that using less than 3 fps can invoke bugs on VLC. VLC has trouble with slow frame rate video from any source