Animation in Python

Animations Using Python

With the use of animation in Python, we can express our data more effectively. Animation is a method in which figures are manipulated to appear as moving images, the simulation of movement created by a series of pictures is animation.

In this article, we will use the Celluloid library which makes animation in Python Matplotlib very simple.

Celluloid – Easy Animation in Python

For beginners, matplotlib animation tutorials can turn out to be complex. Celluloid makes it easy to make animations using matplotlib.

With celluloid, we take “photos” of our visualization to create a frame at each iteration. Once all the frames have been captured we can create an animation with one call. View the readme for more details.

You can install the celluloid library in Python using the Python pip command

pip install celluloid

Steps to create animations using celluloid

Once you are ready with the library, let’s get started with the animations.

1. Import the Camera class from celluloid

First, we need to import the Camera class from the celluloid module and create a camera object by passing the Matplotlib figure object.

from celluloid import Camera
fig = plt.figure()
camera = Camera(fig)

2. Create snapshots while looping over data

Looping to plot data on the Matplotlib figure incrementally and taking the snapshot using .snap( ) method of the camera object.

#plotting data using loops and creating snapshot at each iteration
plt.plot(..)
camera.snap()

3. Create the animation object

Using .animate( ) method of the camera class after all the frames have been created.

#Applying the animate method to create animations
animation = camera.animate()

#Saving the animation
animation.save('my_animation.mp4')

Example Implementation of Animation in Python

Let’s now clearly understand the above steps by creating an animation in Python on tracing a sine function.

#Importing required libraries
%matplotlib notebook
import matplotlib.pyplot as plt
import numpy as np
from celluloid import Camera
import ffmpeg


#Creating Data
x = np.linspace(0, 10, 100)

#defining a function to return sine of input values.
def fun(i):
    y = np.sin(i)
    return y

x_y = fun(x)

#Creating matplotlib figure and camera object
fig = plt.figure()
plt.xlim(0,10)
plt.ylim(-2,2)
camera = Camera(fig)

#Looping the data and capturing frame at each iteration
for i in x:
    plt.plot(x,x_y , color = 'green' , lw = 0.8)
    f = plt.scatter(i, fun(i) , color = 'red' , s = 200)
    plt.title('tracing a sin function')
    camera.snap()

#Creating the animation from captured frames
animation = camera.animate(interval = 200, repeat = True,
                           repeat_delay = 500)



Tracing a sine function

In the code above, we defined a function fun( ) which takes numerical values in and returns the sine of that input value.

As we have our camera object ready, we loop through the data, with each iteration we pass new coordinates of the tracer (dot in red color) and create a snap of the output image.

After capturing all the frames, we apply .animate( ) method with the following input arguments:

  • interval – time between two frames in milliseconds.
  • repeat – (Bool) specify if we want to keep repeating the animation.
  • repeat_delay – if repeat is True, using this we specify time delay to repeat the animation.
#Saving the animation
animation.save('sine_wave.mp4')

Some Limitations of using this library:

  • Make sure the axes limits are the same for all the plots.
  • Pass the artists to the legend function to draw them separately as legends will accumulate from previous plots.

Conclusion

in this article, we discovered a very simple method for creating animation in Python using the celluloid library. Play around with the library to learn it better and become more efficient with it! Happy Learning !! 🙂