Matplotlib - Triangulations



Triangulation is a process of dividing a complex shape or area into simpler parts using triangles. Imagine you have a shape, like a polygon (a closed figure with straight sides), and you want to understand or represent it in a more easy way.

Triangulation involves drawing non-overlapping triangles inside that shape, connecting its vertices (corner points) with straight lines. By doing this, you break down the original shape into a series of triangles −

Triangulations

Triangulations in Matplotlib

In Matplotlib, triangulation refers to the process of dividing a set of points into triangles for visualization purposes. This is useful when you want to create plots having unstructured data.

We can perform triangulation on a set of points in Matplotlib using the "Triangulation" class in the "matplotlib.tri module". Once you have the triangulation, you can use it to create plots, such as 3D surfaces or contour plots, that accurately represent the shape of your data.

Let us start by creating a triangulated surface plot.

Triangulated Surface Plot

A triangulated surface plot in Matplotlib is a 3D visualization that represents a surface using triangles. The term "triangulated" refers to the process of dividing the surface into a network of triangles, creating a simplified mesh that connects data points. This type of plot is generally used when scattered data points need to be visualized as a continuous surface.

Example

In the following example, we are creating a 3D surface plot using a set of points and Delaunay triangulation. The 'x', 'y', and 'z' arrays represent coordinates and heights of points. The Triangulation() function from "matplotlib.tri" module is used to define triangles based on these points −

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

# Creating a set of points
x = np.array([0, 1, 2, 3, 4, 5])
y = np.array([0, 1, 0, 1, 0, 1])
z = np.array([1, 2, 1, 2, 1, 2])

# Defining triangulation using Delaunay triangulation
triang = mtri.Triangulation(x, y)

# Creating a custom triangulated surface plot
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.plot_trisurf(triang, z, cmap='viridis', edgecolor='black')

# Adding labels
ax.set_title('Triangulated Surface Plot')
ax.set_xlabel('X-axis')
ax.set_ylabel('Y-axis')
ax.set_zlabel('Z-axis')

plt.show()

Output

After executing the above code, we get the following output −

Triangulated Surface Plot

Delaunay Triangulation for Scatter Plot

A delaunay triangulation for scatter plot in Matplotlib is a method of connecting a set of scattered data points with non-overlapping triangles in a manner that maximizes the minimum angle of all the triangles.

Example

In here, we are generating 20 random data points and performing Delaunay triangulation using matplotlib.tri module. The triplot() function is used to visualize the Delaunay triangulation with blue dashed lines, and the scatter() function displays the random data points in red color −

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

# Generating random data points
np.random.seed(42)
x = np.random.rand(20)
y = np.random.rand(20)

# Performing Delaunay triangulation
triang = mtri.Triangulation(x, y)

# Creating a scatter plot with Delaunay triangulation
plt.figure()
plt.title('Delaunay Triangulation Scatter Plot')
plt.triplot(triang, 'bo--')
plt.scatter(x, y, c='red', marker='o')

plt.show()

Output

Following is the output of the above code −

Delaunay Triangulation for Scatter Plot

Contour Plot with Triangulation

A contour plot with triangulation in Matplotlib involves creating a visualization where contour lines are drawn over a surface defined by a set of points. The triangulation process is used to connect these points with triangles, forming a mesh that helps in representing the surface more smoothly.

Example

Now, we generate 20 random data points along with their corresponding function values. Next, we create a triangulated mesh using Delaunay triangulation. The triplot() function is used to display the Delaunay triangulation, while "tricontourf" generates a filled contour plot based on the function values −

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

# Generating random data points
np.random.seed(42)
x = np.random.rand(20)
y = np.random.rand(20)
z = np.sin(x * y)

# Defining triangulation using Delaunay triangulation
triang = mtri.Triangulation(x, y)

# Creating a contour plot with triangulation
plt.figure()
plt.title('Contour Plot with Triangulation')
plt.triplot(triang, 'ko-')
plt.tricontourf(triang, z, cmap='plasma')

plt.show()

Output

Output of the above code is as follows −

Contour Plot with Triangulation

Triangulated Line Plot

A triangulated line plot in Matplotlib involves creating a visual representation where lines are drawn to connect a set of points using triangles, forming a triangulated network. This type of plot is useful for representing data as a continuous curve, especially when the points are irregularly spaced.

Example

In the example below, we are using the triplot() function to visualize the Delaunay triangulation with red lines, creating a line plot that connects the data points −

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

# Generating random data points
np.random.seed(42)
x = np.random.rand(20)
y = np.random.rand(20)

# Defining triangulation using Delaunay triangulation
triang = mtri.Triangulation(x, y)

# Creating a line plot with triangulation
plt.figure()
plt.title('Triangulated Line Plot')
plt.triplot(triang, 'ro-')

plt.show()

Output

The output obtained is as shown below −

Triangulated Line Plot
Advertisements