I recently explored a few multiple time region data visualisation techniques that work best in video format. Here is a few short notes on how to get generate high-quality videos using matplotliband ffmpeg.

The standard aspect ratio for modern video is 16:9. It is easy to ensure this ratio using the figsize argument.

import matplotlib.pyplot as plt
fig = plt.figure(figsize=(16, 9))
ax = fig.add_subplot(1, 1, 1)

I usually iteratively generate and save a number of figures in PNG format. This can lead to memory leaks if a new plot is created in every iteration. Instead, use ax.clear() inside the loop and save the same figure object repeatedly.

If you want your video to be HD 1080p quality, use of a dpi=120 argument in the plt.savefig command will give you exactly 1080x1920 resolution when figsize=(16, 9).

There are various parameters that can be used to adjust the encoding behaviour of H.264, after a bit of experimentation I found this to work well for YouTube:

ffmpeg -framerate 30 -pattern_type glob -i '<your-image-dir>/*.png' -c:v libx264 -preset veryslow -tune animation -crf 18 -movflags +faststart -r 30 <output-name>.mp4

Adjust the framerate parameter to suit whatever speed you intend your plots to be shown at. Also, note that the order provided by glob might be according to the file creation date, so you may want to name your files with sequential indexers and use syntax similar to the following instead:

ffmpeg -framerate 24 -i '<your-image-dir>/<filename-template>_%05d.png' -c:v libx264 -preset veryslow -tune animation -crf 18 -movflags +faststart -r 24 <output-name>.mp4