10.4: Creating Subplots
- Page ID
- 136713
\( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)
\( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)
\( \newcommand{\dsum}{\displaystyle\sum\limits} \)
\( \newcommand{\dint}{\displaystyle\int\limits} \)
\( \newcommand{\dlim}{\displaystyle\lim\limits} \)
\( \newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\)
( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\)
\( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\)
\( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\)
\( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\)
\( \newcommand{\Span}{\mathrm{span}}\)
\( \newcommand{\id}{\mathrm{id}}\)
\( \newcommand{\Span}{\mathrm{span}}\)
\( \newcommand{\kernel}{\mathrm{null}\,}\)
\( \newcommand{\range}{\mathrm{range}\,}\)
\( \newcommand{\RealPart}{\mathrm{Re}}\)
\( \newcommand{\ImaginaryPart}{\mathrm{Im}}\)
\( \newcommand{\Argument}{\mathrm{Arg}}\)
\( \newcommand{\norm}[1]{\| #1 \|}\)
\( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\)
\( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\AA}{\unicode[.8,0]{x212B}}\)
\( \newcommand{\vectorA}[1]{\vec{#1}} % arrow\)
\( \newcommand{\vectorAt}[1]{\vec{\text{#1}}} % arrow\)
\( \newcommand{\vectorB}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)
\( \newcommand{\vectorC}[1]{\textbf{#1}} \)
\( \newcommand{\vectorD}[1]{\overrightarrow{#1}} \)
\( \newcommand{\vectorDt}[1]{\overrightarrow{\text{#1}}} \)
\( \newcommand{\vectE}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash{\mathbf {#1}}}} \)
\( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)
\(\newcommand{\longvect}{\overrightarrow}\)
\( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)
\(\newcommand{\avec}{\mathbf a}\) \(\newcommand{\bvec}{\mathbf b}\) \(\newcommand{\cvec}{\mathbf c}\) \(\newcommand{\dvec}{\mathbf d}\) \(\newcommand{\dtil}{\widetilde{\mathbf d}}\) \(\newcommand{\evec}{\mathbf e}\) \(\newcommand{\fvec}{\mathbf f}\) \(\newcommand{\nvec}{\mathbf n}\) \(\newcommand{\pvec}{\mathbf p}\) \(\newcommand{\qvec}{\mathbf q}\) \(\newcommand{\svec}{\mathbf s}\) \(\newcommand{\tvec}{\mathbf t}\) \(\newcommand{\uvec}{\mathbf u}\) \(\newcommand{\vvec}{\mathbf v}\) \(\newcommand{\wvec}{\mathbf w}\) \(\newcommand{\xvec}{\mathbf x}\) \(\newcommand{\yvec}{\mathbf y}\) \(\newcommand{\zvec}{\mathbf z}\) \(\newcommand{\rvec}{\mathbf r}\) \(\newcommand{\mvec}{\mathbf m}\) \(\newcommand{\zerovec}{\mathbf 0}\) \(\newcommand{\onevec}{\mathbf 1}\) \(\newcommand{\real}{\mathbb R}\) \(\newcommand{\twovec}[2]{\left[\begin{array}{r}#1 \\ #2 \end{array}\right]}\) \(\newcommand{\ctwovec}[2]{\left[\begin{array}{c}#1 \\ #2 \end{array}\right]}\) \(\newcommand{\threevec}[3]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \end{array}\right]}\) \(\newcommand{\cthreevec}[3]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \end{array}\right]}\) \(\newcommand{\fourvec}[4]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \\ #4 \end{array}\right]}\) \(\newcommand{\cfourvec}[4]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \\ #4 \end{array}\right]}\) \(\newcommand{\fivevec}[5]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \\ #4 \\ #5 \\ \end{array}\right]}\) \(\newcommand{\cfivevec}[5]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \\ #4 \\ #5 \\ \end{array}\right]}\) \(\newcommand{\mattwo}[4]{\left[\begin{array}{rr}#1 \amp #2 \\ #3 \amp #4 \\ \end{array}\right]}\) \(\newcommand{\laspan}[1]{\text{Span}\{#1\}}\) \(\newcommand{\bcal}{\cal B}\) \(\newcommand{\ccal}{\cal C}\) \(\newcommand{\scal}{\cal S}\) \(\newcommand{\wcal}{\cal W}\) \(\newcommand{\ecal}{\cal E}\) \(\newcommand{\coords}[2]{\left\{#1\right\}_{#2}}\) \(\newcommand{\gray}[1]{\color{gray}{#1}}\) \(\newcommand{\lgray}[1]{\color{lightgray}{#1}}\) \(\newcommand{\rank}{\operatorname{rank}}\) \(\newcommand{\row}{\text{Row}}\) \(\newcommand{\col}{\text{Col}}\) \(\renewcommand{\row}{\text{Row}}\) \(\newcommand{\nul}{\text{Nul}}\) \(\newcommand{\var}{\text{Var}}\) \(\newcommand{\corr}{\text{corr}}\) \(\newcommand{\len}[1]{\left|#1\right|}\) \(\newcommand{\bbar}{\overline{\bvec}}\) \(\newcommand{\bhat}{\widehat{\bvec}}\) \(\newcommand{\bperp}{\bvec^\perp}\) \(\newcommand{\xhat}{\widehat{\xvec}}\) \(\newcommand{\vhat}{\widehat{\vvec}}\) \(\newcommand{\uhat}{\widehat{\uvec}}\) \(\newcommand{\what}{\widehat{\wvec}}\) \(\newcommand{\Sighat}{\widehat{\Sigma}}\) \(\newcommand{\lt}{<}\) \(\newcommand{\gt}{>}\) \(\newcommand{\amp}{&}\) \(\definecolor{fillinmathshade}{gray}{0.9}\)Sometimes we want to show multiple plots in one figure window without placing them on top of each other. For that, we use subplot. A subplot divides one figure window into a grid of smaller plotting areas. The general format is:
subplot(r, c, n)
The first input, r, is the number of rows of plots. The second input, c, is the number of columns of plots. The third input, n, tells MATLAB which subplot position should be active. MATLAB counts the positions across each row, starting from the upper-left corner. For example, for a 2-by-2 subplot:
|
subplot command |
Layout created |
Active position |
|
subplot(2,2,1) |
2 rows by 2 columns |
upper-left plot |
|
subplot(2,2,2) |
2 rows by 2 columns |
upper-right plot |
|
subplot(2,2,3) |
2 rows by 2 columns |
lower-left plot |
|
subplot(2,2,4) |
2 rows by 2 columns |
lower-right plot |
Subplots are helpful when you want to compare related graphs side by side. For example, you might use subplots to compare experimental results from different test cases, temperatures from different cities, or the same function plotted with different numbers of points.
Comparing 10 Points and 20 Points
The following example uses subplots to compare what happens when we plot sin(x) using 10 points versus 20 points.
% Demonstrating subplots using a for loop
clear; clc; clf;
for i = 1:2
x = linspace(0, 2*pi, 10*i);
y = sin(x);
subplot(1, 2, i)
plot(x, y, 'ko')
ylabel('sin(x)')
xlabel('x')
title(sprintf('%d Points', 10*i))
end
Solution

The first subplot uses 10 points and the second subplot uses 20 points. With more points, the shape of the sine wave becomes easier to recognize. This is an important lesson: plots depend on the data points used to create them.
Keeping the Same Axis Limits
When we compare two or more subplots, we often want the axes to use the same scale. If each subplot has a different x-axis or y-axis range, the comparison can become misleading. A curve may look steeper, flatter, larger, or smaller simply because MATLAB chose different axis limits.
For this reason, when subplots are meant to be compared directly, it is a good habit to use the same x-limits and y-limits for all of them. The functions xlim and ylim control the visible limits of the x-axis and y-axis.
xlim([xmin xmax])
ylim([ymin ymax])
You can also use axis to set both x and y limits at the same time.
axis([xmin xmax ymin ymax])
For example, the following script compares sin(x) and cos(x) in two subplots while keeping the same axis limits.
Comparing sin(x) and cos(x) in two subplots while keeping the same axis limits.
% Subplots with matching axis limits
clear; clc; clf;
x = linspace(0, 2*pi, 50);
y1 = sin(x);
y2 = cos(x);
subplot(1, 2, 1)
plot(x, y1, 'b')
title('sin(x)')
xlabel('x')
ylabel('y')
xlim([0 2*pi])
ylim([-1.5 1.5])
grid on
subplot(1, 2, 2)
plot(x, y2, 'r')
title('cos(x)')
xlabel('x')
ylabel('y')
xlim([0 2*pi])
ylim([-1.5 1.5])
grid on
Solution

Good comparison habit: When subplots are used for comparison, keep the same axis limits unless there is a specific reason not to. This helps the reader compare shapes and values fairly.
Another useful command is sgtitle, which adds one title above all subplots in a figure.
sgtitle('Comparison of Sine and Cosine')
Use individual titles for each subplot when each graph needs its own label, and use sgtitle when the entire figure needs one shared title.

