Unstructured Triangular Grid Visualization

PythonPythonBeginner
Practice Now

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

Introduction

In this lab, we will create a triangular 3D contour plot using Matplotlib. This plot is useful for visualizing unstructured triangular grids. We will be using the same data as in the second plot of trisurf3d_2.

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`"]) python(("`Python`")) -.-> python/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`"]) 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 matplotlib/importing_matplotlib -.-> lab-49005{{"`Unstructured Triangular Grid Visualization`"}} matplotlib/figures_axes -.-> lab-49005{{"`Unstructured Triangular Grid Visualization`"}} python/booleans -.-> lab-49005{{"`Unstructured Triangular Grid Visualization`"}} python/lists -.-> lab-49005{{"`Unstructured Triangular Grid Visualization`"}} python/tuples -.-> lab-49005{{"`Unstructured Triangular Grid Visualization`"}} python/importing_modules -.-> lab-49005{{"`Unstructured Triangular Grid Visualization`"}} python/numerical_computing -.-> lab-49005{{"`Unstructured Triangular Grid Visualization`"}} python/data_visualization -.-> lab-49005{{"`Unstructured Triangular Grid Visualization`"}} end

Import necessary libraries

We will start by importing the necessary libraries for this lab, which include Matplotlib and NumPy.

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

Define variables

We will define the variables that we will use to create our plot. These variables include the number of angles, number of radii, and the minimum radius.

n_angles = 48
n_radii = 8
min_radius = 0.25

Create mesh and compute x, y, z

We will create the mesh in polar coordinates and compute x, y, z using the defined variables.

radii = np.linspace(min_radius, 0.95, n_radii)
angles = np.linspace(0, 2*np.pi, n_angles, endpoint=False)
angles = np.repeat(angles[..., np.newaxis], n_radii, axis=1)
angles[:, 1::2] += np.pi/n_angles

x = (radii*np.cos(angles)).flatten()
y = (radii*np.sin(angles)).flatten()
z = (np.cos(radii)*np.cos(3*angles)).flatten()

Create custom triangulation

We will create a custom triangulation using the x and y coordinates.

triang = tri.Triangulation(x, y)

Mask off unwanted triangles

We will mask off the unwanted triangles using the mean of the x and y coordinates.

triang.set_mask(np.hypot(x[triang.triangles].mean(axis=1),
                         y[triang.triangles].mean(axis=1))
                < min_radius)

Create 3D contour plot

We will create a 3D contour plot using the created triangulation and the z coordinates. We will also customize the view angle so it's easier to understand the plot.

ax = plt.figure().add_subplot(projection='3d')
ax.tricontour(triang, z, cmap=plt.cm.CMRmap)
ax.view_init(elev=45.)
plt.show()

Summary

In this lab, we created a triangular 3D contour plot using Matplotlib. We went through the steps of importing the necessary libraries, defining variables, creating a mesh, creating a custom triangulation, masking off unwanted triangles, and creating the 3D contour plot.

Other Python Tutorials you may like