contourf — Create filled contour plots in MATLAB and RunMat.
contourf creates filled contour plots by coloring regions between contour levels. It returns a contour-fill handle and shares contour-level generation semantics with contour in MATLAB and RunMat.
Syntax
h = contourf(Z)
h = contourf(Z, V)
h = contourf(Z, Name, Value, ...)
h = contourf(Z, V, Name, Value, ...)
h = contourf(X, Y, Z)
h = contourf(X, Y, Z, V)
h = contourf(X, Y, Z, V, Name, Value, ...)Inputs
| Name | Type | Required | Default | Description |
|---|---|---|---|---|
Z | NumericArray | Yes | — | Contour height grid. |
V | Any | Yes | — | Contour level count/value vector. |
props | Any | Variadic | — | Name/value contour options. |
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 filled contour plot. |
Errors
| Identifier | When | Message |
|---|---|---|
RunMat:contourf:InvalidArgument | Contour input arrays, level arguments, or name/value options are invalid. | contourf: invalid argument |
RunMat:contourf:Internal | Internal filled-contour render preparation fails unexpectedly. | contourf: internal operation failed |
How contourf works
contourf(Z)uses implicit axes, whilecontourf(X, Y, Z)accepts explicit coordinates.- The returned value is a contour-fill handle in the plotting object system.
- Level-list, level-count, and related contour workflows mirror the broader contour family semantics.
contourfworks naturally withcolorbarandcolormapbecause filled regions are fundamentally color-mapped scalar ranges.- Filled contours and optional line overlays are handled through the shared contour stack.
Examples
Create a filled contour plot
[X, Y] = meshgrid(-2:0.25:2, -2:0.25:2);
Z = X .* exp(-X.^2 - Y.^2);
contourf(X, Y, Z);
colorbar;Use explicit levels with a colormap
[X, Y] = meshgrid(-3:0.2:3, -3:0.2:3);
Z = cos(X) .* sin(Y);
contourf(X, Y, Z, [-0.75 -0.25 0 0.25 0.75]);
colormap('turbo');Inspect the returned contour-fill handle
[X, Y] = meshgrid(1:20, 1:20);
Z = X - Y;
h = contourf(X, Y, Z);
get(h, 'Type')Expected output:
ans =
'contour'Filled scalar field with explicit levels
[X, Y] = meshgrid(linspace(-3, 3, 150), linspace(-3, 3, 150));
Z = sin(X) .* cos(Y) + 0.5*sin(2*X + Y);
contourf(X, Y, Z, 25);
colormap('turbo');
colorbar;
title('Interference Pattern — Filled Contours');
xlabel('x');
ylabel('y');
axis equal;
Using contourf with coding agents
Open a RunMat example with live inputs, then ask the agent to explain how contourf changes the result.
Run a small contourf example, explain the result, then change one input and compare the output.
FAQ
What's the difference between contourf and contour?⌄
contourf fills the regions between contour levels with solid colors from the colormap. contour draws only the boundary lines. If you're building a heatmap-style visualization where color bands carry meaning, contourf is the right choice. If you just need iso-lines overlaid on something else, use contour.
How does the colormap interact with contourf?⌄
Each filled region maps to a range in the active colormap based on its contour level. Calling colormap('hot') after contourf recolors all regions immediately. Pair it with colorbar to show the value-to-color mapping.
contourf(X, Y, Z, 15);
colormap('hot');
colorbar;How do I control the number of filled levels?⌄
Pass an integer as the fourth argument to set the level count: contourf(X, Y, Z, 20) creates 20 levels. For exact control, pass a vector of level values instead: contourf(X, Y, Z, [-1 -0.5 0 0.5 1]). More levels give smoother color transitions but add rendering cost on large grids.
Related Plotting functions
More plotting resources
Open-source implementation
Unlike proprietary runtimes, every RunMat function is open-source. Read exactly how contourf is executed, line by line, in Rust.
- View the source for contourf 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.