Chart Gallery

/

Contour Plot in Python

Contour Plots in Python

A contour plot (also called a contour graph) in python draws iso-value lines across a 2D field so ridges, basins, and gradients in the surface become visible. With xy you build an interactive contour plot from a NumPy grid: choose the number of levels, fill between them or keep clean lines, and pan, zoom, and hover work by default.

Jump to creating a contour plot, levels and fills, or the options table.

Create a Contour Plot

Pass a 2D array of values to contour, with optional x and y coordinates. Set levels to control how many iso-value bands are drawn and filled=True to shade between them:

Line Contours at Explicit Levels

Keep filled=False for clean iso-lines and pass levels an explicit list to place contours at exact values — useful when specific thresholds matter — with width thickening the strokes:

Signed Fields with Dashed Negatives

For a field that crosses zero, set dash_negative=True so negative contours are dashed, use more levels to resolve the structure, and add an xy.colorbar() to tie levels back to values:

Levels, Filled, and Line Contours

levels sets how many iso-value bands the field is sliced into — more levels resolve finer structure. Keep filled=False for clean contour lines, or set filled=True to shade each band with the colormap. Use dash_negative to distinguish contours below zero, which is handy for signed fields.

Contour Plot Options

OptionPurpose
levelsNumber of iso-value bands, or an explicit list of level values.
filledTrue to shade between contours, False for lines only.
colormapNamed color scale mapping levels to color.
widthContour line stroke width in pixels.
opacityContour opacity from 0 to 1.
dash_negativeDash contours with negative values to distinguish sign.

Add to show the level-to-color scale. Pass column names with data= instead of arrays when your data is a table.

API Reference

xy.contour_chart

A contour chart composing `contour` marks.

Props

PropTypeDescription
*childrenComponent

Marks, axes, annotations, and chart chrome.

titleOptional[str]

Title shown above the plot.

widthint | str

Chart width in pixels or a CSS size such as ``"100%"``.

heightint | str

Chart height in pixels or a CSS size such as ``"100%"``.

paddingUnion[float, Sequence[float], None]

Plot margins, as one value or a sequence of side values. Use zero for an edge-to-edge sparkline.

dataTableLike

Chart-level data used by marks that omit their own ``data``.

class_nameOptional[str]

CSS class applied to the chart container.

class_namesOptional[dict[str, str]]

CSS classes keyed by stable chart DOM slot.

styleOptional[dict[str, StyleValue]]

Inline style overrides for the chart container.

stylesOptional[dict[str, dict[str, StyleValue]]]

Inline style mappings keyed by stable chart DOM slot.

on_hoverOptional[Callable[[dict], None]]

Callback receiving hover event payloads.

on_clickOptional[Callable[[dict], None]]

Callback receiving picked-mark click payloads.

on_brushOptional[Callable[[dict], None]]

Callback receiving brush event payloads.

on_selectOptional[Callable[[Selection], None]]

Callback receiving data-space selections.

on_view_changeOptional[Callable[[dict], None]]

Callback receiving viewport change payloads.

hoverOptional[bool]

Whether pointer movement emits hover events.

clickOptional[bool]

Whether picked marks emit click events.

selectOptional[bool]

Whether shift-drag box selection is enabled.

brushOptional[bool]

Whether brush selection is enabled.

crosshairOptional[bool]

Whether plot-aligned hover guides are shown.

navigationOptional[bool]

Whether browser pan and zoom navigation is enabled.

panOptional[bool]

Whether plain-drag panning is enabled.

pan_axesOptional[tuple[str, ...]]

Declared axis IDs translated by pan gestures.

zoomOptional[bool]

Whether viewport zoom is enabled.

default_drag_actionOptional[DefaultDragAction]

Initial action performed by a plain plot drag.

zoom_axesOptional[tuple[str, ...]]

Declared axis IDs changed by zoom gestures and controls.

zoom_limitsOptional[ZoomLimits]

Minimum and maximum magnification globally or by axis.

wheel_zoomOptional[bool]

Whether wheel and trackpad zoom is available.

box_zoomOptional[bool]

Whether box zoom is available as a drag action.

zoom_buttonsOptional[bool]

Whether modebar Zoom In/Out commands are available.

double_click_resetOptional[bool]

Whether double-click restores ``reset_axes``.

reset_axesOptional[tuple[str, ...]]

Declared axis IDs restored by reset.

link_groupOptional[str]

Identifier used to synchronize charts in the browser.

link_axesOptional[tuple[str, ...]]

Axes synchronized within the link group.

FAQ

How do I make a contour plot in Python?

Pass a 2D array to xy.contour(z, x=x, y=y) inside xy.contour_chart(...) and render it. The contour chart supports pan, zoom, and hover out of the box.

What is the difference between a filled and a line contour?

filled=False draws only the iso-value lines; filled=True shades each band between contours with the colormap for a continuous surface look.

How do I control the number of contour levels?

Set levels to an integer for that many evenly spaced bands, or pass an explicit list of values to place contours at exact levels.

How do I show negative contours differently?

Set dash_negative=True so contours with negative values are dashed, making the sign of a signed field easy to read.

Built with Reflex