Plate 02 · Contour plot
Contour plots: when lines of equal value beat a heatmap, and how to set the levels
A contour plot draws lines that join points of equal value across two axes, like the height lines on a hiking map. It shows a third variable, such as temperature, probability or elevation, over a plane. Filled contours colour the bands between the lines.
When to use one
For a smooth surface z = f(x, y). Contours show the shape of the surface: peaks, valleys, ridges, saddle points. Close lines mean a steep change, wide spacing a gentle one. A heatmap shows the same data as colour, which reads better for exact values in a grid; contours read better for shape and gradients, and they survive greyscale printing because the lines carry the information.
For dense scatter data. When thousands of points overlap, you can estimate their density and draw contours around regions of equal density. Claus Wilke, in Fundamentals of Data Visualization, notes that this works well when density changes slowly across both axes, and that drawing small, semi-transparent points over the contours keeps the raw data visible.
When not to
Wilke shows the limit: coloured contour sets for several groups in one panel work for two or three well-separated groups. With more, or with groups that overlap, the lines cross into an unreadable tangle, and one panel per group works better.
Contours also hide detail between the lines. If the surface has sharp local features smaller than the spacing between levels, they disappear.
Choosing the levels
The levels you draw change what the reader sees, and plotting libraries do not all interpret “12 levels” the same way:
- In matplotlib, an integer passed to
levelsis a hint: the library picks no more than n + 2 “nice” boundaries. An array draws exactly those values. The default corresponds to 7 levels. - In Plotly,
ncontoursis a maximum; the actual number is chosen automatically and can be smaller. To set exact levels, givecontoursastart,endandsize.
We met this while building the figures below: the same “12 levels” gave about 12 bands in matplotlib and ggplot2 but far fewer in Plotly and gnuplot, until the levels were set explicitly. If two figures must be compared, set the levels by value, not by count.
For filled contours of a quantity that runs from low to high, use a perceptually uniform sequential colour map such as viridis or cividis, and a diverging map when the data vary around a meaningful centre such as zero (Thyng et al., 2016). You can check a map in the Colormap Lab.
Making it
In matplotlib, contourf fills the bands and contour draws the lines; passing the same levels to
both keeps them aligned:
import numpy as np
import matplotlib
matplotlib.use("Agg")
import matplotlib.pyplot as plt
x = np.linspace(-3, 3, 61)
y = np.linspace(-3, 3, 61)
X, Y = np.meshgrid(x, y)
Z = np.exp(-(X**2 + Y**2) / 2) - 0.5 * np.exp(-((X - 1.5)**2 + (Y - 1)**2))
fig, ax = plt.subplots(figsize=(4, 3.2))
filled = ax.contourf(X, Y, Z, levels=12, cmap="viridis")
ax.contour(X, Y, Z, levels=12, colors="black", linewidths=0.4)
fig.colorbar(filled, ax=ax, label="z")
ax.set_xlabel("x")
ax.set_ylabel("y")
fig.tight_layout()
fig.savefig("contour-matplotlib.png", dpi=150)
The same surface in Plotly, gnuplot, ggplot2 and PLplot is below. Their code, including the explicit levels for Plotly and gnuplot, is in Plot Rosetta.
The same figure in 5 libraries
Same data, labels and colour map. Each image was produced by the code in Plot Rosetta, run on 2026-11-28 (Ubuntu 24.04.5 LTS).

matplotlib Python · 3.11.2

Plotly Python · 7.2.0

gnuplot gnuplot · 6.0 patchlevel 0

ggplot2 R · 3.4.4

PLplot Python · 5.15.0
Sources
- Fundamentals of Data Visualization, chapter 18: Handling overlapping pointsClaus O. Wilke
- matplotlib.pyplot.contourMatplotlib documentation
- Contour traces in Python (reference)Plotly documentation
- True Colors of Oceanography: Guidelines for Effective and Accurate Colormap SelectionThyng et al., Oceanography 29(3), 2016