Triangulation Visualization with triplot(x, y)ΒΆ

The triplot() method displays the Delaunay triangulation of a set of points as a wireframe mesh of triangles. It takes only x and y coordinates (no z-values) and draws the edges and vertices of the triangulation, showing how matplotlib connects scattered points into a triangular mesh.

Why this matters for data science: Understanding the underlying triangulation is important when working with any of the tri* family of plotting functions (tricontour, tricontourf, tripcolor), because the quality of the triangulation directly affects the quality of your visualization. Long, thin (β€œsliver”) triangles produce artifacts, while well-shaped triangles produce smooth interpolations. triplot() lets you inspect the mesh before overlaying data on it. It is also useful in computational geometry, mesh generation for numerical simulations, and spatial analysis where Delaunay triangulations and Voronoi diagrams are fundamental data structures. The example generates 256 random points uniformly distributed in a square, and the resulting triangulation reveals the characteristic pattern of a Delaunay mesh – no point lies inside the circumscribed circle of any triangle.

import matplotlib.pyplot as plt
import numpy as np

plt.style.use('_mpl-gallery-nogrid')

# make data:
np.random.seed(1)
x = np.random.uniform(-3, 3, 256)
y = np.random.uniform(-3, 3, 256)
z = (1 - x/2 + x**5 + y**3) * np.exp(-x**2 - y**2)

# plot:
fig, ax = plt.subplots()

ax.triplot(x, y)

ax.set(xlim=(-3, 3), ylim=(-3, 3))

plt.show()