Animate a 3D Wireframe Plot

PythonPythonBeginner
Practice Now

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

Introduction

In this lab, we will learn how to animate a 3D wireframe plot using Matplotlib, a popular data visualization library in Python.

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/AdvancedPlottingGroup(["`Advanced Plotting`"]) python(("`Python`")) -.-> python/ControlFlowGroup(["`Control Flow`"]) python(("`Python`")) -.-> python/DataStructuresGroup(["`Data Structures`"]) python(("`Python`")) -.-> python/ModulesandPackagesGroup(["`Modules and Packages`"]) 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/AdvancedPlottingGroup -.-> matplotlib/3d_plots("`3D Plots`") python/ControlFlowGroup -.-> python/conditional_statements("`Conditional Statements`") python/ControlFlowGroup -.-> python/for_loops("`For Loops`") python/DataStructuresGroup -.-> python/tuples("`Tuples`") python/ModulesandPackagesGroup -.-> python/importing_modules("`Importing Modules`") python/ModulesandPackagesGroup -.-> python/standard_libraries("`Common Standard Libraries`") 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-49032{{"`Animate a 3D Wireframe Plot`"}} matplotlib/figures_axes -.-> lab-49032{{"`Animate a 3D Wireframe Plot`"}} matplotlib/3d_plots -.-> lab-49032{{"`Animate a 3D Wireframe Plot`"}} python/conditional_statements -.-> lab-49032{{"`Animate a 3D Wireframe Plot`"}} python/for_loops -.-> lab-49032{{"`Animate a 3D Wireframe Plot`"}} python/tuples -.-> lab-49032{{"`Animate a 3D Wireframe Plot`"}} python/importing_modules -.-> lab-49032{{"`Animate a 3D Wireframe Plot`"}} python/standard_libraries -.-> lab-49032{{"`Animate a 3D Wireframe Plot`"}} python/numerical_computing -.-> lab-49032{{"`Animate a 3D Wireframe Plot`"}} python/data_visualization -.-> lab-49032{{"`Animate a 3D Wireframe Plot`"}} python/build_in_functions -.-> lab-49032{{"`Animate a 3D Wireframe Plot`"}} end

Import Libraries

The first step is to import the necessary libraries: Matplotlib, NumPy, and time.

import time
import matplotlib.pyplot as plt
import numpy as np

Set up the Figure and Subplot

The second step is to set up the figure and subplot. We will create a 3D plot using add_subplot.

fig = plt.figure()
ax = fig.add_subplot(projection='3d')

Create a Meshgrid

The third step is to create a meshgrid using linspace.

xs = np.linspace(-1, 1, 50)
ys = np.linspace(-1, 1, 50)
X, Y = np.meshgrid(xs, ys)

Set Z Axis Limits

The fourth step is to set the z axis limits so they aren't recalculated each frame.

ax.set_zlim(-1, 1)

Animate the Plot

The fifth step is to animate the plot. We will use a for loop to iterate through a range of values for phi. In each iteration, we will remove the previous line collection, generate new data, plot the new wireframe, and pause briefly before continuing.

wframe = None
tstart = time.time()
for phi in np.linspace(0, 180. / np.pi, 100):
    if wframe:
        wframe.remove()
    Z = np.cos(2 * np.pi * X + phi) * (1 - np.hypot(X, Y))
    wframe = ax.plot_wireframe(X, Y, Z, rstride=2, cstride=2)
    plt.pause(.001)

Display the Average FPS

The sixth step is to display the average frames per second (FPS) using the total time it took to run the animation.

print('Average FPS: %f' % (100 / (time.time() - tstart)))

Summary

In this lab, we learned how to animate a 3D wireframe plot using Matplotlib. We used a for loop to iterate through a range of values for phi, generated new data, plotted the new wireframe, and paused briefly before continuing. Finally, we displayed the average frames per second (FPS) using the total time it took to run the animation.

Other Python Tutorials you may like