subplot — Select subplot grid locations to create multi-panel axes layouts with MATLAB-compatible subplot behavior.
subplot(m, n, p) selects or creates an axes position inside an m-by-n figure grid and returns its handle. It follows MATLAB-compatible subplot indexing and current-axes selection behavior.
Syntax
ax = subplot(rows, cols, position)Inputs
| Name | Type | Required | Default | Description |
|---|---|---|---|---|
rows | SizeArg | Yes | — | Number of subplot rows. |
cols | SizeArg | Yes | — | Number of subplot columns. |
position | SizeArg | Yes | — | 1-based subplot position in row-major order. |
Returns
| Name | Type | Description |
|---|---|---|
ax | NumericScalar | Encoded handle for the selected subplot axes. |
Errors
| Identifier | When | Message |
|---|---|---|
RunMat:subplot:InvalidArgument | Rows/cols/position are unsupported, non-scalar, non-finite, or non-positive. | subplot: invalid argument |
RunMat:subplot:IndexOutOfRange | Position is outside the configured subplot grid. | subplot: subplot index is out of range for the grid |
RunMat:subplot:Internal | Internal subplot state operation fails. | subplot: internal operation failed |
How subplot works
- The returned value is an axes handle that can be passed explicitly to later plotting commands.
- Subplot-local metadata stays isolated per axes: titles, legends, limits, log modes, and 3-D view state are all tracked independently.
- Subsequent plotting commands operate on the selected subplot unless another axes handle is passed explicitly.
- Indices are one-based in the MATLAB style.
subplot(2, 2, 3)selects the third slot in row-major order.
Examples
Create a 2x2 subplot layout
subplot(2, 2, 1);
plot(0:0.1:1, (0:0.1:1).^2);
subplot(2, 2, 2);
scatter(1:5, [5 4 3 2 1]);Capture and reuse an axes handle explicitly
ax = subplot(1, 2, 2);
plot(ax, 0:0.1:1, sin(0:0.1:1));
get(ax, 'Type')Expected output:
ans =
'axes'Show that subplot-local state stays isolated
subplot(1, 2, 1);
semilogx(logspace(0, 2, 50), logspace(0, 2, 50));
subplot(1, 2, 2);
plot(1:5, 1:5);Expected output:
% Only the first subplot uses a logarithmic x-axisUsing subplot with coding agents
Open a RunMat example with live inputs, then ask the agent to explain how subplot changes the result.
Run a small subplot example, explain the result, then change one input and compare the output.
FAQ
How do I control the number of rows and columns in a subplot layout?⌄
The first two arguments to subplot(m, n, p) set the grid dimensions: m rows and n columns. The third argument p selects which slot to activate, counted in row-major order starting at 1. subplot(3, 1, 2) gives you the middle row of a three-row single-column layout.
Can two subplots share the same x-axis range?⌄
Set matching limits on both axes manually with axis or set. There's no automatic linkaxes yet, but explicit limits keep them in sync.
subplot(2, 1, 1); plot(0:10, rand(1, 11)); axis([0 10 0 1]);
subplot(2, 1, 2); plot(0:10, rand(1, 11)); axis([0 10 0 1]);Can I make subplots of unequal sizes?⌄
You can span multiple grid slots by passing a vector as the third argument. subplot(2, 2, [1 2]) creates a subplot that spans the entire top row of a 2x2 grid, while the bottom row keeps two separate panels.
Related Plotting functions
More plotting resources
Open-source implementation
Unlike proprietary runtimes, every RunMat function is open-source. Read exactly how subplot is executed, line by line, in Rust.
- View the source for subplot 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.