Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Contour plot

Draw iso-level contours over a 2D scalar field using marching squares. Useful for topology, terrain, response surfaces, and 2D density visualisations where the lines themselves are the feature.

The demo draws the bivariate normal density Sir Francis Galton popularised in his 1885 quincunx + regression-board demonstration — a single radial Gaussian peak with five nested iso-density contours. The same shape underlies modern density estimation and 2D KDEs.

Source: Galton board — Wikipedia

Public surface

use wisp_chart::contour::ContourPlot;

let field: Vec<f32> = sample_function_on_grid();  // rows × cols
let plot = ContourPlot::new(
    field,
    /* cols */ 48,
    /* rows */ 48,
    vec![0.15, 0.35, 0.55, 0.75, 0.9],   // iso-levels
);
let g = plot.emit_graphics(&theme, viewport);

Marching squares

Each grid cell's four corners are compared to the iso-level threshold; the 16 possible above/below sign-bit combinations map to 16 line-segment cases. The implementation is a flat match — no degenerate ambiguities are smoothed (saddle points pick a fixed orientation), which keeps the output deterministic across runs and platforms.

Contour vs. filled heatmap

  • A 2D histogram or table heatmap shows the bulk of the field via colour — easy to read overall shape.
  • A contour plot shows specific levels — easy to read "everything above 0.75 is here". Compose both: filled heatmap underneath, contour lines on top.