Create Figure with Multiple Graphs Using Subplots

This example shows how to create a figure containing multiple graphs using the subplot function. The syntax, subplot(m,n,p), divides the figure into an m-by-n grid with an axes in the pth grid location. The grids are numbered along each row.

Create Subplots and Add Subplot Titles

Use subplot to create a figure containing a 2-by-2 grid of graphs. Plot a sine wave in the first subplot.

x = linspace(-5,5); % define x
y1 = sin(x); % define y1

figure % create new figure
subplot(2,2,1) % first subplot
title('First subplot')

Plot another sine wave in the second subplot.

y2 = sin(2*x); % define y2

subplot(2,2,2) % second subplot
title('Second subplot')

Plot two more sine waves in the third and fourth subplots.

y3 = sin(4*x); % define y3
y4 = sin(6*x); % define y4

subplot(2,2,3) % third subplot
title('Third subplot')

subplot(2,2,4) % fourth subplot
title('Fourth subplot')

Add Subplot Axis Labels

Add subplot labels using the xlabel and ylabel functions. By default, xlabel and ylabel label the current axes. The current axes is typically the last axes created or clicked with the mouse. Reissuing the command, subplot(m,n,p), makes the pth subplot the current axes.

Make the third subplot the current axes. Then, label its x-axis and y-axis.


The figure contains four axes with a sine wave plotted in each axes.

See Also

| | | | |

Related Examples

Was this topic helpful?