Filled Contours on Unstructured Grids with tricontourf(x, y, z)ΒΆ
The tricontourf() method draws filled contour regions on irregularly spaced data, combining the unstructured triangulation approach of tricontour() with the filled-region rendering of contourf(). Each region between two contour levels is filled with a solid color from the active colormap.
Why this matters for data science: Filled contours on unstructured data provide a complete spatial picture of a scalar field without requiring gridded input. They are essential for geospatial analysis (interpolating between weather stations), finite element analysis visualization (where meshes are inherently triangular), and any scenario where you need a continuous color representation of scattered measurements. The example plots the raw sample points as grey dots to show where the data actually exists, while the filled contours reveal the interpolated surface between those points. Comparing tricontourf() output with contourf() on the same function highlights how the irregular sampling pattern affects the visual fidelity of the result β denser sampling regions produce more accurate contours, while sparse regions may show triangulation artifacts.
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)
levels = np.linspace(z.min(), z.max(), 7)
# plot:
fig, ax = plt.subplots()
ax.plot(x, y, 'o', markersize=2, color='grey')
ax.tricontourf(x, y, z, levels=levels)
ax.set(xlim=(-3, 3), ylim=(-3, 3))
plt.show()