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
rows- Type
SizeArg- Required
- Yes
- Description
- Number of subplot rows.
cols- Type
SizeArg- Required
- Yes
- Description
- Number of subplot columns.
position- Type
SizeArg- Required
- Yes
- Description
- 1-based subplot position in row-major order.
Returns
ax- Type
NumericScalar- Description
- Encoded handle for the selected subplot axes.
Errors
RunMat:subplot:InvalidArgument- When
- Rows/cols/position are unsupported, non-scalar, non-finite, or non-positive.
- Message
subplot: invalid argument
RunMat:subplot:IndexOutOfRange- When
- Position is outside the configured subplot grid.
- Message
subplot: subplot index is out of range for the grid
RunMat:subplot:Internal- When
- Internal subplot state operation fails.
- Message
subplot: internal operation failed
RunMat:subplot:TooManyOutputs- When
- More than one output is requested.
- Message
subplot: at most one output is supported
Behavior and compatibility
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.
The documented grid controls use single or double values. RunMat can additionally parse typed-integer scalar controls exactly when RunMat extensions are enabled; strict compatibility mode rejects that extra form before changing figure state.
Placement
Graphics state remains host-owned. Resident control values reject before provider access; resident plotted arrays retain the rendering pipeline.
Whole-call resource accounting, cooperative large host controls, and native/browser presentation require integrated qualification.
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 caption: % Only the first subplot uses a logarithmic x-axis
subplot(1, 2, 1);
semilogx(logspace(0, 2, 50), logspace(0, 2, 50));
subplot(1, 2, 2);
plot(1:5, 1:5);Using 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. RunMat also provides linkaxes for supported linked-axis groups; explicit matching limits remain available as illustrated below.
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?⌄
No. This implementation requires a scalar subplot position; vector positions that span multiple grid slots are not supported. Use separate scalar positions to create the individual panels. A vector position is rejected before configuring the subplot.
Can the grid controls be typed integers?⌄
The compatibility surface documents single and double controls. If RunMat extensions are enabled, all eight native integer scalar classes are also accepted and parsed exactly; strict compatibility mode rejects that extra form.
Related Plotting functions
2D Charts
area · bar · errorbar · heatmap · hist · histogram · loglog · pie · plot · scatter · semilogx · semilogy · stairs · stem
Other
addpoints · ancestor · animatedline · autumn · axes · barh · bone · caxis · cla · clf · clim · colorcube · colororder · cool · copper · copyobj · cylinder · daspect · datacursormode · dataTipTextRow · drawnow · fcontour · figure · fill · fill3 · findobj · fsurf · gobjects · gray · groot · hgload · hgsave · hidden · histogram2 · hold · hot · inferno · isgraphics · ishandle · jet · line · lines · linkaxes · magma · openfig · opengl · pan · parula · patch · pink · plasma · plotmatrix · plotyy · polarhistogram · polarplot · polarscatter · print · quiver3 · ribbon · saveas · savefig · sphere · spring · stackedplot · subtitle · summer · suptitle · text · textscatter · textscatter3 · triplot · turbo · viridis · waitbar · winter · wordcloud · xlabel · xlim · xline · xscale · xtickangle · xtickformat · xticklabels · xticks · ylabel · ylim · yline · yscale · ytickangle · ytickformat · yticklabels · yticks · zlim · zoom
More plotting resources
About RunMat
RunMat is a runtime that executes MATLAB-syntax code fast on any GPU. The runtime is free for individuals, including freelancers; students and faculty at an academic institution, for teaching or research there; contributors to an open-source project, for work on that project; organizations with 250 or fewer people and US$10M or less in annual revenue; and anyone, for non-production use. See runmat.com/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.