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.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL matplotlib(("`Matplotlib`")) -.-> matplotlib/BasicConceptsGroup(["`Basic Concepts`"]) matplotlib(("`Matplotlib`")) -.-> matplotlib/PlottingDataGroup(["`Plotting Data`"]) python(("`Python`")) -.-> python/ControlFlowGroup(["`Control Flow`"]) python(("`Python`")) -.-> python/DataStructuresGroup(["`Data Structures`"]) python(("`Python`")) -.-> python/ModulesandPackagesGroup(["`Modules and Packages`"]) python(("`Python`")) -.-> python/PythonStandardLibraryGroup(["`Python Standard Library`"]) python(("`Python`")) -.-> python/DataScienceandMachineLearningGroup(["`Data Science and Machine Learning`"]) python(("`Python`")) -.-> python/FunctionsGroup(["`Functions`"]) matplotlib/BasicConceptsGroup -.-> matplotlib/importing_matplotlib("`Importing Matplotlib`") matplotlib/BasicConceptsGroup -.-> matplotlib/figures_axes("`Understanding Figures and Axes`") matplotlib/PlottingDataGroup -.-> matplotlib/heatmaps("`Heatmaps`") python/ControlFlowGroup -.-> python/for_loops("`For Loops`") python/DataStructuresGroup -.-> python/tuples("`Tuples`") python/DataStructuresGroup -.-> python/sets("`Sets`") python/ModulesandPackagesGroup -.-> python/importing_modules("`Importing Modules`") python/ModulesandPackagesGroup -.-> python/standard_libraries("`Common Standard Libraries`") python/PythonStandardLibraryGroup -.-> python/math_random("`Math and Random`") python/DataScienceandMachineLearningGroup -.-> python/numerical_computing("`Numerical Computing`") python/DataScienceandMachineLearningGroup -.-> python/data_visualization("`Data Visualization`") python/FunctionsGroup -.-> python/build_in_functions("`Build-in Functions`") subgraph Lab Skills matplotlib/importing_matplotlib -.-> lab-48541{{"`Simple Matplotlib Animation Tutorial`"}} matplotlib/figures_axes -.-> lab-48541{{"`Simple Matplotlib Animation Tutorial`"}} matplotlib/heatmaps -.-> lab-48541{{"`Simple Matplotlib Animation Tutorial`"}} python/for_loops -.-> lab-48541{{"`Simple Matplotlib Animation Tutorial`"}} python/tuples -.-> lab-48541{{"`Simple Matplotlib Animation Tutorial`"}} python/sets -.-> lab-48541{{"`Simple Matplotlib Animation Tutorial`"}} python/importing_modules -.-> lab-48541{{"`Simple Matplotlib Animation Tutorial`"}} python/standard_libraries -.-> lab-48541{{"`Simple Matplotlib Animation Tutorial`"}} python/math_random -.-> lab-48541{{"`Simple Matplotlib Animation Tutorial`"}} python/numerical_computing -.-> lab-48541{{"`Simple Matplotlib Animation Tutorial`"}} python/data_visualization -.-> lab-48541{{"`Simple Matplotlib Animation Tutorial`"}} python/build_in_functions -.-> lab-48541{{"`Simple Matplotlib Animation Tutorial`"}} end

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