If you are creating animations with Matplotlib’s fantastic matplotlib.animation object, chances are you’ll also want to save that animation.

Saving can be in video or .gif format. Then you can showcase it, send it to friends, include it in a report, publish it on your website, make a Youtube video etc. the possibilities are endless with beautiful matplotlib animations.

Scroll down and enjoy!

Holy Python is reader-supported. When you buy through links on our site, we may earn an affiliate commission.

Used Where?

  • Saving animated charts or matplotlib animations as gif, avi, mov, mp4 etc.

Let’s start with creating a matplotlib animation, if you need to see more details about it there is a link below.

This tutorial shows “How to save a matplotlib animation” 

if you’d like to see a tutorial about “How to create a matplotlib animation”, you can check out this tutorial here.

Below is a code extract that can be used to create a matplotlib animation:

anim = animation.FuncAnimation(figure, func=update_figure, fargs=(bar_rects, iteration), frames=generator, interval=100, repeat=False)

Estimated Time

15 mins

Skill Level

Intermediate

Important Parameters

.Funcanimation, .save
PillowWriter, FFMpegWriter
fps, writer, save_count

Libraries

matplotlib.animation

Creating the Animation

And here is the animation we get:

Matplotlib Merge Sort Animation

Saving the Animation

Quite a few mishaps can occur when saving a Matplotlib Animation. In this article we’ll look at some of the most common options and parameters that can save you lots of time and prevent fruitless effort. 

1) Deciding on the format

Depending on your intended use you might want to save your matplotlib animation as gif, mp4, avi, mov, etc file formats.

Also depending on the format, the saving parameters will be different.

Saving method can produce a number of confusing errors, so we will cover those little details and parameters regarding each format.

a- Saving a GIF file

Usually the most confusing part when saving an animation as a .gif file is the writer parameter.

f = r"c://Users/xx/Desktop/animation.gif" 
writergif = animation.PillowWriter(fps=30) 
anim.save(f, writer=writergif)

Here is a rundown on some of the most common options:

ImageMagick vs PillowWriter:

Although ImageMagick is suggested as go to Writer Instance when saving in .gif format it can be complicated especially for Windows Users.

Chances are, ImageMagick is already installed on your computer if you are using some flavor of Unix, and its likely not installed if you are using Windows as mentioned on the ImageMagick installation page here.

So what to do? It depends; if you have Windows, you can simply use PillowWriter and it should solve your problem. If you’d like to use ImageMagick still, you can do so by installing it. If you’re running a Unix OS then you should probably be okay to use ImageMagick from the get go. In either case PillowWriter is a great hassle-free option. See example below:

Saving Matplotlib animation in .gif format:

Matplotlib Oscillator Visualization with random oscillations

b - Saving a video file

Saving Matplotlib animation in .mp4 format:

f = r"c://Users/xx/Desktop/animation.mp4" 
writervideo = animation.FFMpegWriter(fps=60) 
anim.save(f, writer=writervideo)

Saving Matplotlib animation in .avi format:

f = r"c://Users/xx/Desktop/animation.avi" 
writervideo = animation.FFMpegWriter(fps=60)
anim.save(f, writer=writervideo)

Saving Matplotlib animation in .mov format:

f = r"c://Users/xx/Desktop/animation.mov" 
writervideo = animation.FFMpegWriter(fps=60) 
anim.save(f, writer=writervideo)

2) Installing ffmpeg (if needed)

This is a common issue for many people, sometimes they don’t have the ffmpeg packages on their computer and this causes problem when trying to generate a video file with Python or its libraries.

Installing ffmpeg is simple: Just navigate to the official download page here. Make sure you chose the appropriate OS for your computer (Linux-Windows-Mac available). Once you extract the contents or installed the package you will have an executable ffmpeg.exe file in the following path: ffmpeg\bin\ffmpeg.exe

In most situations pointing to this path with following command will solve the problem.

Pointing ffmpeg_path to the correct ffmpeg executable file (if needed)

Include the code below somewhere right after importing the matplotlib library as such:

  • make sure you type the right path on your computer.
  • also make sure the path is pointing to the executable file and not just to the folder.
import matplotlib as mpl 
mpl.rcParams['animation.ffmpeg_path'] = r'C:\\Users\\xx\\Desktop\\ffmpeg\\bin\\ffmpeg.exe'
		

3) Output file (gif or video) ends prematurely

Another error that occurs is that you might realize your video or gif file is cut in half or ends prematurely  before the actual animation is finished properly.

This is usually because save_count parameter is not set properly and it defaults to 100 frames which doesn’t cover the full animation. The fix is simple, check out the example below and notice how save_count is adjusted to 1500 frames based on the animation’s length.

anim = animation.FuncAnimation(figure, func=update_figure, fargs=(bar_rects, iteration), frames=generator, interval=100, repeat=True, save_count=1500)

4) Runtime Error (Parameter related)

Passing in values for arguments fps, codec, bitrate, extra_args, or metadata is not supported when writer is an existing MovieWriter instance. These should instead be passed as arguments when creating the MovieWriter instance.

This error occurs when you create the MovieWrites separately and still try to pass values for certain parameters (such as fps, codec, bitrate etc.) inside the save method. Once you create a MovieWriter instance, you should pass the fps parameter inside the instance itself and not inside the save method as below:

Colorful Raindrop animation (inspired by Matplotlib's raindrop animation)
f = r"c://Users/xx/Desktop/animation.mp4" 
writermp4 = animation.FFMpegWriter() 
anim.save(f, writer=writermp4,fps=60)
f = r"c://Users/xx/Desktop/animation.mp4" 
writermp4 = animation.FFMpegWriter(fps=60) 
anim.save(f, writer=writermp4)

5) Window size / Resolution:

You can define the size of your figure window while creating your plot as in the code below. This will also be the window size of your video or gif file.

Just adjust the figsize parameter to your liking and convenience.

plt.subplots(figsize=(8,6))

Conclusion

That’s it!

We hope you enjoyed this tutorial. Animated charts can give your visualization the live spirit it needs to capture the attention of the audience or communicate your message much much better.

For more tutorials like this: Python Visualization Tutorials.

Recommended Posts