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.

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