3D Wireframe Plotting

PythonPythonBeginner
Practice Now

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

Introduction

This lab is designed to show you how to create a 3D wireframe plot in Python using Matplotlib. A wireframe plot is a visual representation of a three-dimensional surface that displays the structure of the surface using lines. In this lab, we will show how to set the rstride and cstride parameters to control the density of the lines in the plot.

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/BasicConceptsGroup(["`Basic Concepts`"]) 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`"]) matplotlib/BasicConceptsGroup -.-> matplotlib/importing_matplotlib("`Importing Matplotlib`") matplotlib/BasicConceptsGroup -.-> matplotlib/figures_axes("`Understanding Figures and Axes`") matplotlib/AdvancedPlottingGroup -.-> matplotlib/3d_plots("`3D Plots`") python/BasicConceptsGroup -.-> python/variables_data_types("`Variables and Data Types`") python/DataStructuresGroup -.-> python/tuples("`Tuples`") python/DataStructuresGroup -.-> python/dictionaries("`Dictionaries`") python/ModulesandPackagesGroup -.-> python/importing_modules("`Importing Modules`") python/ModulesandPackagesGroup -.-> python/using_packages("`Using Packages`") python/PythonStandardLibraryGroup -.-> python/data_collections("`Data Collections`") python/DataScienceandMachineLearningGroup -.-> python/data_visualization("`Data Visualization`") subgraph Lab Skills matplotlib/importing_matplotlib -.-> lab-49033{{"`3D Wireframe Plotting`"}} matplotlib/figures_axes -.-> lab-49033{{"`3D Wireframe Plotting`"}} matplotlib/3d_plots -.-> lab-49033{{"`3D Wireframe Plotting`"}} python/variables_data_types -.-> lab-49033{{"`3D Wireframe Plotting`"}} python/tuples -.-> lab-49033{{"`3D Wireframe Plotting`"}} python/dictionaries -.-> lab-49033{{"`3D Wireframe Plotting`"}} python/importing_modules -.-> lab-49033{{"`3D Wireframe Plotting`"}} python/using_packages -.-> lab-49033{{"`3D Wireframe Plotting`"}} python/data_collections -.-> lab-49033{{"`3D Wireframe Plotting`"}} python/data_visualization -.-> lab-49033{{"`3D Wireframe Plotting`"}} end

Import necessary libraries

We will start by importing the necessary libraries. In this case, we will use Matplotlib and the axes3d module.

import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import axes3d

Create a figure and two subplots

We will create a figure with two subplots using subplots() method. We will also set the projection to '3d' so that our subplots will be three-dimensional.

fig, (ax1, ax2) = plt.subplots(
    2, 1, figsize=(8, 12), subplot_kw={'projection': '3d'})

Get the test data

We will use the get_test_data() method from axes3d module to get the test data.

X, Y, Z = axes3d.get_test_data(0.05)

Create the first subplot

We will create the first subplot with the rstride parameter set to 10 and cstride parameter set to 0.

ax1.plot_wireframe(X, Y, Z, rstride=10, cstride=0)
ax1.set_title("Column (x) stride set to 0")

Create the second subplot

We will create the second subplot with the rstride parameter set to 0 and cstride parameter set to 10.

ax2.plot_wireframe(X, Y, Z, rstride=0, cstride=10)
ax2.set_title("Row (y) stride set to 0")

Show the plot

We will show the plot using the show() method.

plt.show()

Summary

In this lab, we learned how to create a 3D wireframe plot using Matplotlib. We used the rstride and cstride parameters to control the density of the lines in the plot. We created a figure with two subplots and used the plot_wireframe() method to create the wireframe plot in each subplot. Finally, we showed the plot using the show() method.

Other Python Tutorials you may like