Multiple Plots & Subplots
Comparing data usually means showing several lines together, or several small charts side by side. MATLAB gives you three tools: several lines in one plot call, hold on to add to an existing plot, and subplot (or the newer tiledlayout) to divide a figure into a grid of axes.
Several lines in one call
plot accepts any number of x/y pairs, each with an optional line spec. MATLAB cycles through its default colours (blue, orange, yellow, purple, …) automatically:
x = linspace(0, 2*pi, 100);
plot(x, sin(x), x, cos(x), '--')
legend('sin', 'cos')
xlabel('x')x = linspace(0, 2*pi, 100);
first = x(x < pi/2); % look between 0 and pi/2 only
[~, k] = min(abs(sin(first) - cos(first)));
fprintf('The curves cross near x = %.3f (exactly pi/4 = %.3f)\n', first(k), pi/4);The curves cross near x = 0.762 (exactly pi/4 = 0.785)
The two curves cross at π/4 (and again at 5π/4), but the 100 sampled x-values don't include π/4 exactly, so the closest sample is a little off. That is a good reminder that plotted curves are just straight lines between samples.
hold on
Normally each plot call replaces what is in the axes. hold on keeps the existing content so the next plots are added; hold off returns to the default. This is useful when the lines come from different steps of your code, or when you mix plot types:
years = 2018:2024;
actual = [3.1 3.4 3.3 3.9 4.2 4.4 4.9];
p = polyfit(years, actual, 1); % straight-line trend
trend = polyval(p, years);
plot(years, actual, 'ko', 'MarkerFaceColor', 'k')
hold on
plot(years, trend, 'r-', 'LineWidth', 1.5)
hold off
legend('Measured', 'Linear trend', 'Location', 'northwest')
xlabel('Year'); ylabel('Revenue (M)')years = 2018:2024;
actual = [3.1 3.4 3.3 3.9 4.2 4.4 4.9];
p = polyfit(years, actual, 1);
fprintf('Trend grows by %.3f per year\n', p(1));
fprintf('Trend value in 2025: %.2f\n', polyval(p, 2025));Trend grows by 0.296 per year
Trend value in 2025: 5.07
polyfit and polyval are explained in the Polynomials lesson; here they just provide a second line to draw.
subplot
subplot(m, n, k) splits the figure into an m-by-n grid and makes cell k the current axes. Cells are numbered across the rows, left to right, then top to bottom. Every plotting command after it goes into that cell:
x = linspace(-2, 2, 81);
subplot(2, 2, 1); plot(x, x); title('linear')
subplot(2, 2, 2); plot(x, x.^2); title('quadratic')
subplot(2, 2, 3); plot(x, x.^3); title('cubic')
subplot(2, 2, 4); plot(x, exp(x)); title('exponential')x = linspace(-2, 2, 81);
f = {x, x.^2, x.^3, exp(x)};
names = {'linear', 'quadratic', 'cubic', 'exponential'};
for k = 1:4
fprintf('%-12s y from %7.3f to %6.3f\n', names{k}, min(f{k}), max(f{k}));
endlinear y from -2.000 to 2.000
quadratic y from 0.000 to 4.000
cubic y from -8.000 to 8.000
exponential y from 0.135 to 7.389
Each subplot has its own axes, limits, title and labels. A cell can also span several positions: subplot(2, 2, [3 4]) uses the whole bottom row.
tiledlayout (MATLAB only)
Newer MATLAB versions (R2019b+) offer tiledlayout and nexttile, which handle spacing better and let you add a shared title. Octave doesn't have them, so in code meant for both programs use subplot:
% MATLAB R2019b or later only
tiledlayout(1, 2)
nexttile; plot(1:5, [2 4 3 5 4]); title('Left')
nexttile; bar([3 1 2]); title('Right')Several figure windows
Instead of sharing one figure, you can open separate windows with figure. Each figure call creates a new window and makes it current:
figure; plot(1:10) % figure 1
figure; plot((1:10).^2) % figure 2, figure 1 stays open
figure(1) % switch back to figure 1
close allTest yourself
What does hold on do?
- Pauses the program
- Keeps the current plot so new plots are added to it
- Freezes the axis limits only
- Saves the figure
In subplot(2, 3, 5), which cell is selected?
- Row 2, column 2
- Row 1, column 5
- Row 3, column 2
- Row 2, column 3
How do you draw y1 and y2 against x with a single call?
plot(x, [y1 y2])onlyplot(x, y1, x, y2)plot(x, y1); plot(x, y2)plot2(x, y1, y2)