Triangular 3D Surfaces

PythonPythonBeginner
Practice Now

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

Introduction

In this lab, we will learn how to create a 3D surface with a triangular mesh using Python Matplotlib library.

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 python(("`Python`")) -.-> python/BasicConceptsGroup(["`Basic Concepts`"]) matplotlib(("`Matplotlib`")) -.-> matplotlib/BasicConceptsGroup(["`Basic Concepts`"]) python(("`Python`")) -.-> python/DataStructuresGroup(["`Data Structures`"]) python(("`Python`")) -.-> python/ModulesandPackagesGroup(["`Modules and Packages`"]) python(("`Python`")) -.-> python/DataScienceandMachineLearningGroup(["`Data Science and Machine Learning`"]) python/BasicConceptsGroup -.-> python/comments("`Comments`") matplotlib/BasicConceptsGroup -.-> matplotlib/importing_matplotlib("`Importing Matplotlib`") matplotlib/BasicConceptsGroup -.-> matplotlib/figures_axes("`Understanding Figures and Axes`") python/BasicConceptsGroup -.-> python/booleans("`Booleans`") python/DataStructuresGroup -.-> python/lists("`Lists`") python/DataStructuresGroup -.-> python/tuples("`Tuples`") python/ModulesandPackagesGroup -.-> python/importing_modules("`Importing Modules`") python/DataScienceandMachineLearningGroup -.-> python/numerical_computing("`Numerical Computing`") python/DataScienceandMachineLearningGroup -.-> python/data_visualization("`Data Visualization`") subgraph Lab Skills python/comments -.-> lab-49013{{"`Triangular 3D Surfaces`"}} matplotlib/importing_matplotlib -.-> lab-49013{{"`Triangular 3D Surfaces`"}} matplotlib/figures_axes -.-> lab-49013{{"`Triangular 3D Surfaces`"}} python/booleans -.-> lab-49013{{"`Triangular 3D Surfaces`"}} python/lists -.-> lab-49013{{"`Triangular 3D Surfaces`"}} python/tuples -.-> lab-49013{{"`Triangular 3D Surfaces`"}} python/importing_modules -.-> lab-49013{{"`Triangular 3D Surfaces`"}} python/numerical_computing -.-> lab-49013{{"`Triangular 3D Surfaces`"}} python/data_visualization -.-> lab-49013{{"`Triangular 3D Surfaces`"}} end

Import Libraries

First, we need to import the necessary libraries:

import matplotlib.pyplot as plt
import numpy as np

Define Variables

We will define the variables for the radii and angles:

n_radii = 8
n_angles = 36

Create Radii and Angles Spaces

We will create the radii and angles spaces using the linspace function:

## Make radii and angles spaces (radius r=0 omitted to eliminate duplication).
radii = np.linspace(0.125, 1.0, n_radii)
angles = np.linspace(0, 2*np.pi, n_angles, endpoint=False)[..., np.newaxis]

Convert Polar Coordinates to Cartesian Coordinates

We will convert the polar coordinates to Cartesian coordinates:

## Convert polar (radii, angles) coords to cartesian (x, y) coords.
## (0, 0) is manually added at this stage, so there will be no duplicate
## points in the (x, y) plane.
x = np.append(0, (radii*np.cos(angles)).flatten())
y = np.append(0, (radii*np.sin(angles)).flatten())

Compute Z to Make the Pringle Surface

We will compute z to make the pringle surface:

## Compute z to make the pringle surface.
z = np.sin(-x*y)

Create the 3D Surface

We will create the 3D surface using the plot_trisurf function:

ax = plt.figure().add_subplot(projection='3d')
ax.plot_trisurf(x, y, z, linewidth=0.2, antialiased=True)

plt.show()

Summary

In this lab, we have learned how to create a 3D surface with a triangular mesh using Python Matplotlib library. We have imported the necessary libraries, defined the variables for the radii and angles, created the radii and angles spaces, converted the polar coordinates to Cartesian coordinates, computed z to make the pringle surface, and created the 3D surface using the plot_trisurf function.

Other Python Tutorials you may like