subplot — Split a figure into subplot grids for multi-panel charts, side-by-side comparisons, and MATLAB subplot workflows.
subplot(m, n, p) selects an axes location inside an m by n figure grid and returns an axes handle for that slot. In RunMat, subplot selection is the backbone for axes-local plot state: legends, labels, log modes, view angles, and plot children all attach to the currently selected subplot unless an explicit handle is passed.
How subplot works in RunMat
- 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-axisFAQ
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 functions to explore
These functions work well alongside subplot. Each page has runnable examples you can try in the browser.
More plotting resources
Open-source implementation
Unlike proprietary runtimes, every RunMat function is open-source. Read exactly how subplot works, line by line, in Rust.
- View subplot.rs on GitHub
- Learn how the 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 — faster, on any GPU, with no license required.
- Simulations that took hours now take minutes. RunMat automatically optimizes your math for GPU execution on Apple, Nvidia, and AMD hardware. No code changes needed.
- Start running code in seconds. Open the browser sandbox or download a single binary. No license server, no IT ticket, no setup.
- A full development environment. GPU-accelerated 2D and 3D plotting, automatic versioning on every save, and a browser IDE you can share with a link.