In this tutorial, we’ll try to understand how to plot a 3D surface triangulation plot in Python using the matplotlib library.

### What is a Surface Triangulation Plot?

A triangulation of a compact surface is a finite collection of triangles that cover the surface in such a way that every point on the surface is in a triangle, and the intersection of any two triangles is either void, a common edge, or a common vertex. A triangulated surface is called a tri-surface.

To plot a surface triangulation plot in Python, use the matplotlib `Axes.plot_trisurf`

method.

## Using the `matplotlib.Axes.plot_trisurf()`

method

To plot a 3D surface triangulation plot, use the following steps –

- Create a matplotlib Axes object with a 3D project.
- For the above axes above, plot the surface triangulation plot using the
`plot_trisurf()`

method.

The following is the syntax –

**BasicSyntax:**

Axes.plot_trisurf(*args, color=None, norm=None, vmin=None, vmax=None, lightsource=None, **kwargs)

The (optional) triangulation can be specified in one of two ways; either:

plot_trisurf(triangulation, ...)

where triangulation is a Triangulation object, or:

plot_trisurf(X, Y, ...) plot_trisurf(X, Y, triangles, ...) plot_trisurf(X, Y, triangles=triangles, ...)

**Parameters:**

**X, Y, Z**: Data values as 1D arrays.**color**: Color of the surface patches.

For more details about the parameters refer this.

Now let us understand the above method with some examples.

### Example 1 – 3D surface triangulation plot

import numpy as np import matplotlib.pyplot as plt # Defining variables z = np.linspace(-10000, 10000, 200) x = np.sin(z) y = np.cos(z) # Creating figure and 3D axes fig = plt.figure(figsize =(9, 5)) ax = plt.axes(projection ='3d') # Creating plot ax.plot_trisurf(x, y, z) plt.show()

Output:

The steps followed in the above example are:

- Import required modules
- Define the variables to plot
- Generate the figure and 3D axes
- Plot the 3D surface triangulation plot using
`Axes.plot_trisurf`

.

### Example 2 – 3D surface triangulation plot along with colorbar

import numpy as np import matplotlib.pyplot as plt # Defining variables # Creating radii and angles r = np.linspace(0.125, 1.0, 100) a = np.linspace(0, 2 * np.pi, 100, endpoint = False) # Repeating all angles for every radius a = np.repeat(a[..., np.newaxis], 100, axis = 1) # Creating dataset x = np.append(0, (r * np.cos(a))) y = np.append(0, (r * np.sin(a))) z = (np.sin(x ** 4) + np.cos(y ** 4)) # Creating figure and 3D axes fig = plt.figure(figsize =(9, 5)) ax = plt.axes(projection ='3d') # plot the 3D Surface triangulation trisurf=ax.plot_trisurf(x, y, z,cmap='hot') fig.colorbar(trisurf, ax = ax, shrink = 0.5, aspect = 5) plt.show()

Output:

The steps followed in the above example are:

- Import required modules
- Define the variables to plot
- Generate the figure and 3D axes
- Plot the 3D surface triangulation plot using
`Axes.plot_trisurf`

. - Add the colorabar usinf
`fig.colorbar`

(refer this).

### Example 3 – 3D surface triangulation plot

import numpy as np import matplotlib.pyplot as plt # Defining variables radii = np.linspace(0.125, 1.0, 60) angles = np.linspace(0, 2*np.pi, 60, endpoint=False) x = np.append(0, (radii*np.cos(angles)).flatten()) y = np.append(0, (radii*np.sin(angles)).flatten()) z = np.sin(-x*y) # Creating figure and 3D axes fig = plt.figure(figsize =(9, 5)) ax = plt.axes(projection ='3d') # Creating plot trisurf=ax.plot_trisurf(x, y, z,cmap='hot', linewidth=0.2, antialiased=True) fig.colorbar(trisurf, ax = ax, shrink = 0.5, aspect = 5) plt.show()

Output:

The steps followed in the above example are:

- Import required modules
- Define the variables to plot
- Generate the figure and 3D axes
- Plot the 3D surface triangulation plot using
`Axes.plot_trisurf`

. - Add the colorabar usinf
`fig.colorbar`

(refer this).

### Example 4 – 3D surface triangulation by setting triangles parameter

import numpy as np import matplotlib.pyplot as plt from scipy.spatial import Delaunay # Defining variables radii = np.linspace(0.125, 1.0, 60) angles = np.linspace(0, 2*np.pi, 60, endpoint=False) x = np.append(0, (radii*np.cos(angles)).flatten()) y = np.append(0, (radii*np.sin(angles)).flatten()) z = np.sin(-x*y) triangle = [[x[i],y[i]] for i in range(len(x))] # Creating figure and 3D axes fig = plt.figure(figsize =(9, 5)) ax = plt.axes(projection ='3d') # Creating plot trisurf=ax.plot_trisurf(x, y, z,cmap='hot',triangles=triangle) fig.colorbar(trisurf, ax = ax, shrink = 0.5, aspect = 5) plt.show()

Output:

The steps followed in the above example are:

- Import required modules
- Define the variables to plot
- Generate the figure and 3D axes
- Plot the 3D surface triangulation plot using
`Axes.plot_trisurf`

. - Add the colorabar usinf
`fig.colorbar`

(refer this). - To understand more about triangulation, refer this.
- To understand more about Delaunay triangulation, refer this.

### Example 5 – Sphere using surface triangulation

import numpy as np import matplotlib.pyplot as plt from scipy.spatial import Delaunay # Defining variables a = np.linspace(0, 2 * np.pi,25) b = np.linspace(0, np.pi, 25) [X, Y] = np.meshgrid(a, b) x = np.outer(np.cos(a), np.sin(b)) y = np.outer(np.sin(a), np.sin(b)) z = np.outer(np.ones_like(a), np.cos(b)) # Creating figure and 3D axes fig = plt.figure(figsize =(9, 5)) ax = plt.axes(projection ='3d') # Creating plot ax.plot_trisurf(x.flatten(), y.flatten(), z.flatten(), triangles=Delaunay(np.array([X.flatten(), Y.flatten()]).T).simplices, cmap='winter') plt.show()

Output:

The steps followed in the above example are:

- Import required modules
- Define the variables to plot.
- List a and b contains 25 evenly spaced number generated using numpy.linspace().
- Then create a mesh grid of a and b using numpy.meshgrid().
- List x, y, and z is the outer product of the vectors
`(cos(a) and sin(b)) , (sin(a) and sin(b)) and (array of ones and cos(b))`

generated using numpy.outer() and numpy.ones()

- Generate the figure and 3D axes
- Plot the 3D surface triangulation plot using
`Axes.plot_trisurf`

.

### Example 6 – Ellipsoid using surface triangulation

from mpl_toolkits import mplot3d import numpy as np import matplotlib.pyplot as plt from scipy.spatial import Delaunay # Defining variables #covariance matrix A = np.array([ [2.2, 0.4, 0.2], [0.4, 2.8, 0.3], [0.2, 0.3, 2.4] ]) a = np.linspace(0, 2 * np.pi, 25) b = np.linspace(0, np.pi, 25) [X, Y] = np.meshgrid(a, b) x = np.outer(np.cos(a), np.sin(b)) y = np.outer(np.sin(a), np.sin(b)) z = np.outer(np.ones_like(a), np.cos(b)) E1 = np.zeros((25, 25)) E2 = np.zeros((25, 25)) E3 = np.zeros((25, 25)) for i in range(25): for j in range(25): [E1[i, j], E2[i, j], E3[i, j]] = A @ np.array([x[i, j], y[i, j], z[i, j]]) # Creating figure and 3D axes fig = plt.figure(figsize =(9, 5)) ax = plt.axes(projection ='3d') # Creating plot ax.plot_trisurf( E2.flatten(), E3.flatten(), E1.flatten(), triangles=Delaunay(np.array([X.flatten(), Y.flatten()]).T).simplices, cmap='summer') # fig.colorbar(trisurf, ax = ax, shrink = 0.5, aspect = 5) plt.show()

Output:

The steps followed in the above example are:

- Import required modules
- Define the variables to plot
- List a and b contains 25 evenly spaced number generated using numpy.linspace().
- List x,y, and z is the outer product of the vectors (cos(a) and sin(b)), (sin(a) and sin(b)), and (array of ones and cos(b)) respectively as these arrays are multiplied by covariance matrix A to get the coordinate for plotting the ellipsoid.

- Generate the figure and 3D axes
- Plot the 3D surface triangulation plot using
`Axes.plot_trisurf`

.

You might also be interested in –

- How to Plot a 3D Wireframe Plot in Python?
- How to Create a 3D Plot in Python?
- How to plot a 3D surface plot in Python?
- How to Plot a 3D Contour plot in Python?

**Subscribe to our newsletter for more informative guides and tutorials. ****We do not spam and you can opt out any time.**