Read isolines and filled bands against a regular grid, declared contour levels and explicit missing-cell masks.
Contour plots: respect measured grids and interpolation boundaries
Know what a contour line represents
A contour line joins locations estimated to share one value of a continuous field. A filled contour uses bands between stated levels. Both require a coordinate system and an underlying surface. In a warehouse temperature survey, readings at sensor positions are observations; color between sensors is an interpolation model, not another measurement. If observations sit on an irregular mesh, state how they were gridded before rendering. A heatmap shows cell values directly and may be more honest when only measured cells matter. Contours are appropriate when spatial continuity is scientifically defensible and the uncertainty between samples is acceptable for the question.
Choose levels and preserve the numeric domain
Use one ascending level list across panels that readers will compare. Automatic levels chosen independently for each frame can make unchanged values move to new colors. The legend should give units, exact boundaries and what happens below the first or above the last level. Do not choose dozens of bands merely to create a smooth-looking gradient; more colors do not add measurements. Shared scales and panels apply to contours as well. Preserve the aspect ratio if x and y use comparable spatial units, and label a transformed coordinate axis if a map projection or other conversion changes distance. Separate contour lines from the base grid in an accessible data export.
Mask gaps rather than inventing a bridge
Missing readings can create holes in the surface. Mark unavailable grid cells and state whether the renderer masks a cell, a vertex or any quadrilateral touching it; these policies have different visible footprints. Never replace an absent observation with zero just to satisfy a plotting library. A sensor fault along a wall can create an apparent cold band if imputed as zero. Missing mapped regions present the same honest-versus-smooth choice. Show the sampled domain boundary and avoid extrapolating contours far outside the convex hull of observations unless an explicit physical model supports it.
Treat interpolation as a model to validate
Linear interpolation within each grid cell can create straight segments even when the real field bends; smoothing can conceal localized peaks. Keep point markers or a linked table to show where measurements actually exist. Cross-validate by withholding sensors in several areas, compare predicted values against withheld readings and investigate systematic error near edges. Uncertainty grows where sensors are sparse, while render provenance should record grid version, interpolation method, level list and mask policy. For a decision about safety limits, publish the original measurements and margin near the threshold instead of relying on the contour image alone.
Worked contract
from math import isfinite
def contour_cells(x_coordinates: list[float], y_coordinates: list[float],
values: list[list[float | None]], levels: list[float]):
if len(x_coordinates) < 2 or len(y_coordinates) < 2:
raise ValueError("a contour grid needs at least two points per axis")
if any(not isfinite(position) for position in x_coordinates + y_coordinates):
raise ValueError("coordinates must be finite")
if any(right <= left for left, right in zip(x_coordinates, x_coordinates[1:])):
raise ValueError("x coordinates must increase")
if any(top <= bottom for bottom, top in zip(y_coordinates, y_coordinates[1:])):
raise ValueError("y coordinates must increase")
if len(values) != len(y_coordinates) or any(len(row) != len(x_coordinates) for row in values):
raise ValueError("grid dimensions do not match axes")
if len(levels) < 2 or any(not isfinite(level) for level in levels) or any(
upper <= lower for lower, upper in zip(levels, levels[1:])):
raise ValueError("at least two increasing finite levels are required")
if any(reading is not None and not isfinite(reading)
for row in values for reading in row):
raise ValueError("observed values must be finite")
usable = []
masked = []
for row in range(len(y_coordinates) - 1):
for column in range(len(x_coordinates) - 1):
corners = (values[row][column], values[row][column + 1],
values[row + 1][column], values[row + 1][column + 1])
cell = (column, row)
(usable if all(value is not None for value in corners) else masked).append(cell)
return usable, masked
sensor_grid = [[18.0, 20.0, 22.0], [19.0, None, 24.0], [20.0, 23.0, 25.0]]
usable, masked = contour_cells([0.0, 47.0, 94.0], [0.0, 59.0, 118.0],
sensor_grid, [18.0, 21.0, 24.0, 27.0])
assert usable == []
assert masked == [(0, 0), (1, 0), (0, 1), (1, 1)]Performance and operating cost
Validating a grid with X columns, Y rows and L contour levels takes O(XY + X + Y + L) time and O(XY) output space for the cell audit. An actual contour renderer adds work that depends on its interpolation and line-tracing algorithm. This standard-library check does not draw isolines; it makes the mask rule explicit: any cell touching a missing corner is unavailable. A single missing center reading masks all four neighboring cells in the sample, rather than fabricating a continuous field.
Common Mistakes
- Treating interpolated values as if they were sensor readings.
- Changing levels between comparison panels without announcing the change.
- Filling missing cells with zero and displaying a false low region.
- Drawing contours beyond the sampled domain without a model and uncertainty statement.
Validation drill
Map 47 warehouse sensors on a fixed coordinate system. Hide three sensors, audit the masked cells, compare two fixed level sets, and record held-out interpolation error near the perimeter.
