Matplotlib - Celluloid Library



The celluloid library is a third-party library that works with Matplotlib to create animations. It simplifies the process of creating moving visuals by managing the figure and axis changes for each animation frame. It provides a user-friendly interface, allowing you to focus on the content of each frame rather than dealing with complex animation details.

Matplotlib Celluloid Library

Matplotlib is a useful Python library for creating various visualizations, such as charts and graphs. If you want to add animation to your plots, you can use the celluloid library. It simplifies the process of generating dynamic and interesting animations using Matplotlib. In this tutorial, we will go through the basics of using Matplotlib with the celluloid library to make animated plots.

Before starting, make sure you have installed the required libraries −

pip install matplotlib
pip install celluloid

Creating Matplotlib Animations

Once you have installed the required libraries, let us go through the steps involved in creating Matplotlib animations using celluloid −

Step 1 − Import Libraries

Let us start by importing the necessary libraries i.e. Matplotlib for plotting, NumPy for numerical operations, and the Camera class from the celluloid library.

import matplotlib.pyplot as plt
import numpy as np
from celluloid import Camera

Step 2 − Setting Up Matplotlib Figure and Axis

Next, set up a standard Matplotlib figure and axis, just like you would for a static plot. These will be used to visualize the data in each frame.

fig, ax = plt.subplots()

Step 3 − Initializing the Camera Object

Now, we initialize the "Camera" object from the "celluloid" library. This object will capture each frame for the animation, allowing you to flawlessly create the final animated plot.

camera = Camera(fig)

Step 4 − Update Frames in a Loop

Inside a loop, update the plot for each frame by modifying the data or layout. In this example, the sine wave is shifted horizontally for each frame. The "camera.snap()" method captures the current frame.

num_frames = 50

for i in range(num_frames):
    # Update your plot for each frame
    x = np.linspace(0, 2 * np.pi, 100)
    y = np.sin(x + i * 0.1)
    ax.plot(x, y)

    # Capture the current frame
    camera.snap()

Step 5 − Show Animation

After capturing all frames, use the "animate()" method of the "Camera" object to create the animation and display it.

animation = camera.animate()
plt.show()

Complete Example

Following is the complete example of a simple animated plot. Here, we use the celluloid library in Matplotlib to generate an animated plot of a sine wave. The animation iterates through a specified number of frames (num_frames), updating the sine wave by shifting it horizontally in each frame −

import matplotlib.pyplot as plt
import numpy as np
from celluloid import Camera

# Setting up Matplotlib figure and axis
fig, ax = plt.subplots()
camera = Camera(fig)

# Creating animated plot
num_frames = 50
for i in range(num_frames):
    x = np.linspace(0, 2 * np.pi, 100)
    y = np.sin(x + i * 0.1)
    
    # Updating plot for each frame
    ax.plot(x, y)
    
    # Capturing the frame
    camera.snap()

# Displaying the animation
animation = camera.animate()
plt.show()

The resulting animation shows the sine wave oscillating horizontally, creating a visual representation of a dynamic signal. The celluloid library captures each frame, allowing the animation to be displayed

Animated Scatter Plot

An animated scatter plot is a dynamic visualization that shows points moving or changing over time. Each point on the plot represents data, and as time progresses, the positions of these points changes, creating a visually engaging animation.

Example

In here, we are generating an animated scatter plot using the celluloid library in Matplotlib. The animation iterates through a specified number of frames (num_frames) and updates the scatter plot by generating random x and y coordinates for each frame −

import matplotlib.pyplot as plt
import numpy as np
from celluloid import Camera

# Setting up Matplotlib figure and axis
fig, ax = plt.subplots()
camera = Camera(fig)

# Creating animated scatter plot
num_frames = 50
for i in range(num_frames):
    x = np.random.rand(20)
    y = np.random.rand(20)
    
    # Updating scatter plot for each frame
    ax.scatter(x, y)
    
    # Capturing the frame
    camera.snap()

# Displaying the animated scatter plot
animation = camera.animate()
plt.show()

The resulting animation shows a dynamic set of points moving randomly within the plot. The celluloid library captures each frame, allowing the animated scatter plot to be displayed.

Animated Bar Chart

In an animated bar chart, bars representing different categories fluctuates in height as the animation progresses, creating a visual of how values in each category change over time.

Example

In this example, we are creating an animated bar chart with random bar heights in each frame −

import matplotlib.pyplot as plt
import numpy as np
from celluloid import Camera

# Setting up Matplotlib figure and axis
fig, ax = plt.subplots()
camera = Camera(fig)

# Creating animated bar chart
num_frames = 50
x = np.arange(10)
for i in range(num_frames):
    y = np.random.randint(1, 10, size=10)
    
    # Updating bar chart for each frame
    ax.bar(x, y)
    
    # Capturing the frame
    camera.snap()

# Displaying the animated bar chart
animation = camera.animate()
plt.show()
Advertisements