Lokasi ngalangkungan proxy:   [ UP ]  
[Ngawartoskeun bug]   [Panyetelan cookie]                
Skip to content
No results
  • About AskPython
  • Blog
  • Contact Me
  • Free Online Developer Tools Hub
  • Home
  • Meet the Team
  • Privacy Policy
  • Python
  • Python Course for Beginners Online FREE
  • Python Tutorial – Mega Index
  • Terms and Conditions

🚀 Supercharge your YouTube channel's growth with AI.

Try YTGrowAI Free
  • Home
  • Blog
  • About AskPython
  • Python Course
  • Start Here

Animation in Python

  • Abhishek WasnikAbhishek Wasnik
  • November 30, 2020
  • Python Modules
Animation in 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 !! 🙂

Abhishek Wasnik
Abhishek Wasnik
Articles: 29
Python Pandas between(): Check If a Number is Between Two Numbers
Previous Post Python Pandas between(): Check If a Number is Between Two Numbers
Next Post How to update the value of a row in a Python Dataframe?
How to update the value of a row in a Python Dataframe?

Related Posts

np.linalg.eig: Eigenvalues and Eigenvectors

np.linalg.eig: Eigenvalues and Eigenvectors

  • November 30, 2026
np.sin() in NumPy: Radians, Arrays, and Examples

How to Use np.sin() in NumPy

  • November 30, 2026
cv2.imread: Load Images with OpenCV

cv2.imread: Load Images with OpenCV

  • November 30, 2026

Partner Websites

  • GoLangDocs
  • LinuxForDevices
  • CodeForGeek

Popular Categories

  • Python Programming
  • Python Modules
  • OOPS in Python
  • Python Programming Examples
  • Strings in Python

Hall of Fame

  • Python Programming
  • OOPS in Python
  • Creating Python Modules
  • Args and Kwargs in Python
  • Python Dictionary (Dict) Tutorial
  • Python List – 15 Things You MUST Know
  • Python Set – Things You MUST Know
  • Python String Functions: Methods by Task

Important Links

  • Home
  • Python Course
  • Start Here
  • Blog
  • About AskPython
Latest posts
  • np.linalg.eig: Eigenvalues and Eigenvectors
  • What Does %s Mean in a Python Format String?
  • How to Use np.sin() in NumPy
  • cv2.imread: Load Images with OpenCV
  • pd.read_json: Read JSON Into a DataFrame

Copyright © 2026 - AskPython. All rights reserved.