Matplotlib library of Python is a plotting tool used to plot graphs of functions or figures. It can also be used as an animation tool too. The plotted graphs when added with animations gives a more powerful visualization and helps the presenter to catch a larger number of audience. Matplotlib can also easily connect with Pandas to create even more sophisticated animations.
Animations in Matplotlib can be made by using the
Animation class in two ways:
- By calling a function over and over: It uses a predefined function which when ran again and again creates an animation.
- By using fixed objects: Some animated artistic objects when combined with others yield an animation scene.
It is important to note that we must at all points keep a reference to the animated object or else the animation will stop. This is because the
Animation class holds a single pointer reference to the animation object and as the time advances to run the animation this pointer reference must be kept otherwise it will be collected as a garbage value.
Though there are two ways, the first way is more common and convenient and here, we will make use of that only. However, you can read the documentation of the other as well, here .
Let’s dive into Matplotlib animations.
Download ffmpeg for Python from here.
Let’s check an example. Here we will try and make a continuous sine wave using animations and plotting tools. We will make use of
matplotlib for this. As already said, we will be using the function method as opposed to the artistic objects.
Note: To save an animation to your computer, use
At first, after importing the necessities, we set a blank figure or a blank window on which the entire animation will be drawn. Next we initialize a variable
line which will contain the x and y co-ordinates of the plot. These are kept empty at first as the data in it will continuously keep changing because of the animation. Finally, we state the animation function
animate(i) which takes an argument i, where i is called the frame number and using this we create the sine wave(or any other figure) which will continuously vary depending upon the value of i. In the last line
anim = FuncAnimation(fig, animate, init_func=init, frames=200, interval=20, blit=True) we use the
FuncAnimation function to create an animation which will display 200 frames per second and in an interval of 20 micro secs.
Now that’s a very powerful visualization. One thing to note is that when we view our saved gif, it will be a continuous clip unlike the video in our output which gets terminated in a few seconds. Let’s look at one more example. Try to guess the output as we code the program as it will clear our concept.
As we might already have guessed and as obvious as the saved file name suggests, it’s an animation of a continuously growing coil. Just as before, we first import all the modules. But this time, we import the
matplotlib.animation library completely
import matplotlib.animation as animation
however, in the previous example, we imported just the
FuncAnimation function from it
from matplotlib.animation import FuncAnimation
However, this does not really make any changes and one can choose any way of importing. Then we create a figure on which the animation will be placed. The
animate function varies with the frame number i. One thing to know is that a coil is nothing but a composite function of sine and cos. We take the sine function in x-axis and cos in y-axis and the resultant figure gives a coil.
Hence, we conclude that many interesting animations can be made by using some basic knowledge of
matplotlib. This really comes in handy when one needs to present some visualizations with additional power of animation without using higher level animation tools such as Blender.
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.