Simple Matplotlib Animation Tutorial

MatplotlibMatplotlibBeginner
Practice Now

This tutorial is from open-source community. Access the source code

Introduction

This tutorial will guide you through how to create a simple animation using matplotlib.pyplot. Animations can be useful for visualizing data that changes over time. In this tutorial, we will generate a random set of data and display it as an animation.

VM Tips

After the VM startup is done, click the top left corner to switch to the Notebook tab to access Jupyter Notebook for practice.

Sometimes, you may need to wait a few seconds for Jupyter Notebook to finish loading. The validation of operations cannot be automated because of limitations in Jupyter Notebook.

If you face issues during learning, feel free to ask Labby. Provide feedback after the session, and we will promptly resolve the problem for you.

Import necessary libraries

We need to import the necessary libraries to generate our animation. We will use numpy to generate random data and matplotlib.pyplot to display it as an animation.

import matplotlib.pyplot as plt
import numpy as np

Generate random data

We will generate a 3D array of random data using numpy.random.random(). We will use a seed value to ensure that the same set of data is generated each time the code is run.

np.random.seed(19680801)
data = np.random.random((50, 50, 50))

Create the animation

We will use a for loop to iterate through each frame of the animation. In each iteration, we will clear the axis, plot the current frame, set the title, and pause for a short amount of time to allow the animation to be displayed.

fig, ax = plt.subplots()

for i, img in enumerate(data):
    ax.clear()
    ax.imshow(img)
    ax.set_title(f"frame {i}")
    plt.pause(0.1)

Display the animation

We can display the animation by running the code. The animation will be displayed in a new window.

plt.show()

Summary

In this tutorial, we learned how to create a simple animation using matplotlib.pyplot. We generated a random set of data and displayed it as an animation using a for loop and the plt.pause() function. Animations can be a useful tool for visualizing data that changes over time.

Other Matplotlib Tutorials you may like