contour — Create contour line plots in MATLAB and RunMat.
contour creates contour line plots from scalar fields. It returns a contour handle and supports level-list and level-step forms, sharing contour-generation behavior across MATLAB and RunMat.
Syntax
h = contour(Z)
h = contour(Z, N)
h = contour(Z, V)
h = contour(Z, Name, Value, ...)
h = contour(Z, V, Name, Value, ...)
h = contour(X, Y, Z)All supported contour forms
h = contour(Z)
h = contour(Z, N)
h = contour(Z, V)
h = contour(Z, Name, Value, ...)
h = contour(Z, V, Name, Value, ...)
h = contour(X, Y, Z)
h = contour(X, Y, Z, N)
h = contour(X, Y, Z, V)
h = contour(X, Y, Z, V, Name, Value, ...)
h = contour(X, Y, Z, N, Name, Value, ...)Inputs
| Name | Type | Required | Default | Description |
|---|---|---|---|---|
Z | NumericArray | Yes | — | Contour height grid. |
N | NumericScalar | Yes | — | Requested contour level count. |
V | NumericArray | Yes | — | Explicit contour level values. |
props | Any | Variadic | — | Name/value contour options. |
V | Any | Yes | — | Contour level count/value vector. |
X | NumericArray | Yes | — | X axis vector/meshgrid matrix matching Z rows. |
Y | NumericArray | Yes | — | Y axis vector/meshgrid matrix matching Z columns. |
Returns
| Name | Type | Description |
|---|---|---|
h | NumericScalar | Handle to contour line plot. |
Errors
| Identifier | When | Message |
|---|---|---|
RunMat:contour:InvalidArgument | Contour input arrays, level arguments, or name/value options are invalid. | contour: invalid argument |
RunMat:contour:Internal | Internal contour render preparation fails unexpectedly. | contour: internal operation failed |
How contour works
contour(Z)uses implicit axes, whilecontour(X, Y, Z)accepts explicit vector or matrix coordinates.- The builtin returns a contour handle that works with the plotting property system.
- Levels can be controlled through explicit level lists, level counts, or level-step workflows.
- Contour state is subplot-local and composes naturally with
colormap,colorbar, and surface/composite builtins. - GPU-aware contour generation is used when the shared plotting path can operate directly on GPU-resident scalar fields.
Options
'LevelList'/'Levels'accepts an explicit, strictly increasing vector of contour values.'LevelStep'specifies evenly spaced contour levels.'LineColor'accepts MATLAB color strings, RGB triples, and contour-specific modes such as'auto'or'none'where supported.
Examples
Create a contour line plot from a scalar field
[X, Y] = meshgrid(-2:0.25:2, -2:0.25:2);
Z = X .* exp(-X.^2 - Y.^2);
contour(X, Y, Z);Use explicit contour levels
[X, Y] = meshgrid(-2:0.25:2, -2:0.25:2);
Z = X .* exp(-X.^2 - Y.^2);
contour(X, Y, Z, [0 0.25 0.5]);Inspect the returned contour handle
[X, Y] = meshgrid(1:20, 1:20);
Z = X + Y;
h = contour(X, Y, Z);
get(h, 'Type')Expected output:
ans =
'contour'Temperature-style level sets
[X, Y] = meshgrid(linspace(-3, 3, 120), linspace(-3, 3, 120));
Z = 2*exp(-((X-1).^2 + Y.^2)) + 1.5*exp(-((X+1.5).^2 + (Y-1).^2)) - exp(-(X.^2 + (Y+1.5).^2));
contour(X, Y, Z, 20);
colormap('turbo');
colorbar;
title('Thermal Field — Level Sets');
xlabel('x (m)');
ylabel('y (m)');
axis equal;
Using contour with coding agents
Open a RunMat example with live inputs, then ask the agent to explain how contour changes the result.
Run a small contour example, explain the result, then change one input and compare the output.
FAQ
How do I set specific contour levels?⌄
Pass a vector of values as the fourth argument. Each value becomes one contour line.
contour(X, Y, Z, [0.1 0.3 0.5 0.7 0.9]);If you pass a scalar instead, it's interpreted as the number of levels (e.g., contour(X, Y, Z, 20) draws 20 automatically spaced levels).
Can I add labels to the contour lines?⌄
Use clabel after contour to annotate the lines with their level values. Pass the contour handle to clabel so it knows which plot to label.
[C, h] = contour(X, Y, Z);
clabel(C, h);What's the difference between contour and contourf?⌄
contour draws line-only iso-curves — the regions between lines are transparent. contourf fills those regions with colors from the active colormap. Use contour when you want to overlay on other plots or keep the background visible, and contourf when the filled color bands are the primary visualization.
Related Plotting functions
More plotting resources
Open-source implementation
Unlike proprietary runtimes, every RunMat function is open-source. Read exactly how contour is executed, line by line, in Rust.
- View the source for contour in Rust on GitHub
- Learn how the RunMat runtime works
- Found a bug? Open an issue with a minimal reproduction.
About RunMat
RunMat is an open-source runtime that executes MATLAB-syntax code blazing on any GPU. It is licensed under the Apache 2.0 license.
- RunMat automatically optimizes your math for GPU execution on Apple, Nvidia, and AMD hardware. No code changes needed. Simulations that took hours now take minutes.
- Start running code in seconds. RunMat runs in the browser, on the desktop, or from the CLI. No license server, no IT ticket.