Accelerating the pace of engineering and science

Documentation Center

• Trial Software

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
plot(x,y1)
title('First subplot')
```

Plot another sine wave in the second subplot.

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

subplot(2,2,2) % second subplot
plot(x,y2)
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
plot(x,y3)
title('Third subplot')

subplot(2,2,4) % fourth subplot
plot(x,y4)
title('Fourth subplot')
```

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.

```subplot(2,2,3)
xlabel('x-axis')
ylabel('y-axis')
```

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