1. Home
  2. Data & files

Plotting · contours

Contour Levels: Choosing Values for a Contour Plot

Eigenfield editorial team ·

A contour plot draws the curves along which a function of two variables takes a fixed value. Which values get a curve is entirely the author’s choice, and a poor choice hides the shape of the data: too few levels show a blob, too many bury it in lines. This page covers the common ways of choosing contour levels and a rule for picking round, readable ones. The topic belongs with the plotting techniques in Data and files and connects directly to mesh plots with contours.

Evenly spaced levels

The default in most plotting tools is to divide the data range into equal steps. Given the minimum zmin and maximum zmax of the grid, and a wish for n lines strictly inside the range, the step is (zmax − zmin) / (n + 1), and the levels are zmin plus multiples of the step. For a range from 0 to 10 with n = 4 the step is 10 / 5 = 2, and the levels are 2, 4, 6 and 8.

Equal steps suit quantities where the same difference means the same thing everywhere, such as temperature or height above ground. The lines then crowd together exactly where the surface is steep, which is the information a contour plot exists to convey: spacing between lines is inversely related to slope.

Round numbers read better

Levels such as 1.76, 3.52 and 5.28 are correct and unreadable. Round the step to a nice value, one of 1, 2 or 5 times a power of ten, and start the levels at a multiple of that step. Suppose the data run from 0.3 to 9.1 and about five lines are wanted. The raw step is (9.1 − 0.3) / 5 = 1.76, which rounds to 2. The first multiple of 2 at or above 0.3 is 2, and the last at or below 9.1 is 8, so the levels are 2, 4, 6 and 8: four lines, all easy to label.

import numpy as np
def nice_levels(z, n=5):
    raw = (np.nanmax(z) - np.nanmin(z)) / n
    p = 10 ** np.floor(np.log10(raw))
    step = min((s * p for s in (1, 2, 5, 10) if s * p >= raw), default=raw)
    lo = np.ceil(np.nanmin(z) / step) * step
    hi = np.floor(np.nanmax(z) / step) * step
    return np.arange(lo, hi + step / 2, step)

Running it on the range above returns exactly the list 2, 4, 6, 8. The half-step added to the upper bound protects against rounding error dropping the final level.

Explicit lists and special values

Sometimes particular values matter: a safety threshold, a break-even line, the zero contour of a signed quantity. Pass those directly as a list. A single level is also useful. The zero contour of f(x, y) is the implicit curve f(x, y) = 0, and drawing only that level plots the curve without solving for either variable. For example, x² + y² − 1 = 0 produces a unit circle from a grid of values.

When combining a few chosen values with an even background set, draw the background lines thin and gray and the special ones thick and colored, so the eye finds them at once.

Logarithmic spacing

Data covering several orders of magnitude, such as concentrations or power spectra, crowd into one corner under even steps. Space the levels evenly in the logarithm instead: 1, 10, 100, 1000, or the finer sequence 1, 2, 5, 10, 20, 50, 100. Each line then marks a constant ratio. Take the logarithm of the data first if the plot also uses a log color scale, so that both agree.

How many lines

Between 5 and 15 lines suit most figures. Beyond that, filled bands between levels carry more information than lines do, and a colorbar shows what each band means. Always label at least some lines directly with their values; a contour plot without numbers shows shape but not size.

Where the axes cross also affects how a plot reads, see plot axis intercepts. Surfaces drawn as 3D wireframes with the same levels projected below appear under surface plots with contours.

Back to top