Arranging Multiple Axes with subplot
subplot(m, n, p) divides the current figure into an m-by-n grid of axes and makes the p-th cell (numbered left-to-right, top-to-bottom) the active axes for the next plotting command. For example, subplot(2, 2, 3) creates a 2x2 grid and activates the bottom-left cell. You can also pass a vector for p, such as subplot(2, 2, [1 2]), to merge multiple grid cells into one wider axes spanning the first row. Each call to subplot(m,n,p) with the same m and n reuses the same grid layout, so you typically call it once per panel just before the corresponding plot() command.
Cricket analogy: subplot(2,2,3) picking the bottom-left cell is like a TV broadcast's four-way split screen dedicating one quadrant to the bowler's-eye view while others show the pitch map and scorecard.
figure;
subplot(2,2,1);
plot(x, sin(x)); title('Sine');
subplot(2,2,2);
plot(x, cos(x)); title('Cosine');
subplot(2,2,[3 4]);
plot(x, tan(x)); ylim([-10 10]); title('Tangent (wide panel)');tiledlayout for More Flexible Grids
Introduced in R2019b, tiledlayout(m, n) is the modern alternative to subplot, offering better control over spacing via the 'TileSpacing' and 'Padding' name-value arguments and supporting tiles that span multiple rows or columns via nexttile(span). A typical pattern is t = tiledlayout(2,2,'TileSpacing','compact'); followed by repeated nexttile; plot(...) calls, which automatically advance to the next open tile in order. Unlike subplot, tiledlayout also supports a shared title and shared axis labels for the whole layout via title(t, 'Overall Title') and xlabel(t, 'Shared X Label'), avoiding repetitive per-axes labeling.
Cricket analogy: A shared title(t,'...') across a tiledlayout of four bowlers' spell analyses is like one match-summary header sitting above four separate over-by-over graphs in a post-match report.
Figure-Level Customization
The figure() function itself accepts name-value properties such as 'Position', [left bottom width height] in pixels to control window size and placement, and 'Color', 'w' to set the background color, which matters when exporting for print since MATLAB's default figure background is a light gray. Axis limits are controlled per-axes with xlim([xmin xmax]) and ylim([ymin ymax]), while axis equal forces equal scaling on both axes so that circles appear circular rather than elliptical — critical for plotting geometric data. sgtitle('Overall Title') adds a single title spanning subplot-based grids, serving the same purpose as tiledlayout's title(t,...) for figures built with the older subplot function.
Cricket analogy: axis equal ensuring a circle stays circular is like a pitch-map graphic keeping the field's radial distances proportionally accurate rather than stretching them into an oval.
tiledlayout is generally preferred over subplot in modern MATLAB code (R2019b+) because it handles spacing automatically, supports shared titles/labels, and resizes tiles more gracefully when the figure window is resized.
Mixing subplot and tiledlayout calls within the same figure is not supported — pick one approach per figure, since tiledlayout manages its own internal grid that subplot does not know how to coexist with.
- subplot(m,n,p) creates an m-by-n grid and activates cell p for the next plot command.
- A vector for p in subplot merges multiple grid cells into a wider or taller panel.
- tiledlayout with nexttile is the modern replacement offering compact spacing and shared titles/labels.
- figure('Position', [...]) controls window size and placement in pixels.
- axis equal preserves true geometric proportions, essential for circles and maps.
- sgtitle adds one title spanning an entire subplot-based figure.
- Do not mix subplot and tiledlayout calls within the same figure.
Practice what you learned
1. What does subplot(3, 1, 2) do?
2. Which function is the modern (R2019b+) replacement for subplot with better spacing control?
3. How do you add one title spanning all panels of a tiledlayout named t?
4. What does axis equal accomplish?
5. Can subplot and tiledlayout calls be mixed within the same figure?
Was this page helpful?
You May Also Like
Plotting Basics in MATLAB
Learn how to create, label, and style 2-D line plots in MATLAB using the plot function, and how to control colors, markers, and axes.
Curve Fitting and Interpolation
Learn to fit polynomial and custom models to data with polyfit and fit, and estimate values between known data points using interp1 and related interpolation functions.
Reading and Writing Data Files
Learn MATLAB's core file I/O functions for reading and writing text, CSV, and binary data, including readmatrix, writematrix, and low-level fopen/fprintf.
Related Reading
Related Study Notes in Programming
Browse all study notesApache Spark Study Notes
Programming · 30 topics
ProgrammingApache Flink Study Notes
Programming · 30 topics
ProgrammingHadoop Study Notes
Programming · 30 topics
ProgrammingSnowflake Study Notes
Programming · 30 topics
ProgrammingApache Airflow Study Notes
Programming · 30 topics
Programmingdbt (Data Build Tool) Study Notes
Programming · 30 topics