This post explains how to use subplot in MATLAB with examples. Subplot command is used to create multiple plots in a grid layout.
Syntax
To divide the figure into an m
x n
grid and create an axes / new plot at position p, we can write
subplot(m,n,p)
The first subplot (p
= 1) is the first column of the first row, the second subplot (p
= 2) is the second column of the first row, and so on. This command selects the axes at position p
if the axes already exists.
To create a subplot without aligning it with grid positions, we can write
subplot('Position',position)
where position
is row vector of the form [left bottom width heisght]
. The normalized bottom left coordinates (both range from 0 to 1) are represented by left
and bottom
. The normalized width and height is represented by width
and height
.
Example: Creating 4 subplots in a 2×2 grid
The following code creates 4 different subplots in four quadrants of a 2×2 grid.
%% Example 1 - Creating 4 subplots in a 2x2 grid
f1 = 1/10; % Frequency 1
f2 = 1/20; % Frequency 2
f3 = 1/30; % Frequency 3
f4 = 1/50; % Frequency 4
t = linspace(0,300,1000); % independant variable
signal1 = sin(2*pi*f1*t); % generate signal 1
signal2 = sin(2*pi*f2*t); % generate signal 2
signal3 = sin(2*pi*f3*t); % generate signal 3
signal4 = sin(2*pi*f4*t); % generate signal 4
subplot(2,2,1) % 2x2 grid: Select axes at position 1
plot(t,signal1) % Plot signal 1
title('Subplot 1 (p = 1)')
xlabel('t')
ylabel('signal 1')
subplot(2,2,2) % 2x2 grid: Select axes at position 2
plot(t,signal2) % Plot signal 2
title('Subplot 2 (p = 2)')
xlabel('t')
ylabel('signal 2')
subplot(2,2,3) % 2x2 grid: Select axes at position 3
plot(t,signal3) % Plot signal 3
title('Subplot 3 (p = 3)')
xlabel('t')
ylabel('signal 3')
subplot(2,2,4) % 2x2 grid: Select axes at position 4
plot(t,signal4) % Plot signal 4
title('Subplot 4 (p = 4)')
xlabel('t')
ylabel('signal 4')
Example: Creating 2 subplots in a 2×1 grid
The following code creates 2 different subplots stacked on top of each other in a 2×1 grid.
%% Example 2 - Creating 2 subplots in a 2x1 grid
f1 = 1/10; % Frequency 1
f2 = 1/20; % Frequency 2
t = linspace(0,300,1000); % independent variable
signal1 = sin(2*pi*f1*t); % generate signal 1
signal2 = sin(2*pi*f2*t); % generate signal 2
subplot(2,1,1) % 2x1 grid: Select axes at position 1
plot(t,signal1) % Plot signal 1
title('Subplot 1 (p = 1)')
xlabel('t')
ylabel('signal 1')
subplot(2,1,2) % 2x1 grid: Select axes at position 2
plot(t,signal2) % Plot signal 2
title('Subplot 2 (p = 2)')
xlabel('t')
ylabel('signal 2')
Example: Creating 3 subplots in a 3×2 grid
The following code creates 4 different subplots using a 3×2 grids. Signal 1 and signal 2 are plotted at position 1 and 2 respectively. Position 3 and 4 are combined to create an axes on which signal 3. Signal 4 is plotted on an axes that combines position 5 and 6.
%% Example 3 - Creating 4 subplots in a 3x2 grid
f1 = 1/10; % Frequency 1
f2 = 1/20; % Frequency 2
f3 = 1/30; % Frequency 3
f4 = 1/50; % Frequency 4
t = linspace(0,300,1000); % independent variable
signal1 = sin(2*pi*f1*t); % generate signal 1
signal2 = sin(2*pi*f2*t); % generate signal 2
signal3 = sin(2*pi*f3*t); % generate signal 3
signal4 = sin(2*pi*f4*t); % generate signal 4
subplot(3,2,1) % 3x2 grid: Select axes at position 1
plot(t,signal1) % Plot signal 1
title('Subplot 1 (p = 1)')
xlabel('t')
ylabel('signal 1')
subplot(3,2,2) % 3x2 grid: Select axes at position 2
plot(t,signal2) % Plot signal 2
title('Subplot 2 (p = 2)')
xlabel('t')
ylabel('signal 2')
subplot(3,2,[3 4]) % 3x2 grid: Select axes at position 3 and 4
plot(t,signal3) % Plot signal 3
title('Subplot 3 (p = 3,4)')
xlabel('t')
ylabel('signal 3')
subplot(3,2,[5 6]) % 3x2 grid: Select axes at position 5 and 6
plot(t,signal4) % Plot signal 4
title('Subplot 4 (p = 5,6)')
xlabel('t')
ylabel('signal 4')
Example: Creating subplots at custom positions without a grid
If you don’t want to use the grid layout then you can specify custom positions and dimensions for your plots.
Note: If the new axes overlap existing axes, then the new axes replace the existing axes i.e. previous plot will be deleted.
%% Example 4 - Creating subplots at custom position without a grid
f1 = 1/10; % Frequency 1
f2 = 1/20; % Frequency 2
t = linspace(0,300,1000); % independent variable
signal1 = sin(2*pi*f1*t); % generate signal 1
signal2 = sin(2*pi*f2*t); % generate signal 2
subplot('Position',[0.1 0.1 0.7 0.3]) % Create an axes at custom position
plot(t,signal1) % Plot signal 1
title('Subplot 1')
xlabel('t')
ylabel('signal 1')
subplot('Position',[0.3 0.6 0.3 0.3]) % Create an axes at custom position
plot(t,signal2) % Plot signal 2
title('Subplot 2')
xlabel('t')
ylabel('signal 2')
This concludes this tutorial. You can consult official MATLAB documentation for more details and examples.