Accelerating the pace of engineering and science

# Documentation Center

• Trial Software

# subplot

Create axes in tiled positions

## Syntax

• subplot(m,n,p) example
• subplot(m,n,p,'replace') example
• subplot(m,n,p,'align')
• subplot('Position',positionVector) example
• subplot(___,Name,Value) example

## Description

example

subplot(m,n,p) divides the current figure into an m-by-n grid and creates an axes in the grid position specified by p. MATLAB® numbers its grids by row, such that the first grid is the first column of the first row, the second grid is the second column of the first row, and so on.

example

subplot(m,n,p,'replace') deletes any existing axes in grid location p and creates a new axes.

subplot(m,n,p,'align') creates a new axes so that the plot boxes are aligned. This is the default behavior.

example

subplot('Position',positionVector) creates a new axes at the position specified by positionVector. The positionVector is a four-element vector of the form [left,bottom,width,height], such that the entries are normalized values between 0.0 to 1.0. If the position vector specifies an axes that overlaps any previous axes, then the new axes replaces the existing ones.

example

subplot(___,Name,Value) specifies properties for the axes using any of the input argument combinations in the previous syntaxes and one or more Name,Value pair arguments.

example

h = subplot(___) returns the handle to the axes created by the subplot function.

example

subplot(h) makes the axes with handle h the current axes for subsequent plotting commands.

## Examples

expand all

### Upper and Lower Subplots

Create a figure with two stacked subplots. Plot a sine wave in each axes.

```x = linspace(0,10);
y1 = sin(x);
y2 = sin(5*x);

figure
subplot(2,1,1);
plot(x,y1)

subplot(2,1,2);
plot(x,y2)```

### Quadrant of Subplots

Create a figure divided into four subplots.

Set up four vectors to store sine values of different frequencies.

```x = linspace(0,10);
y1 = sin(x);
y2 = sin(2*x);
y3 = sin(4*x);
y4 = sin(8*x);```

Plot the four sine waves and title each subplot.

```figure
subplot(2,2,1);
plot(x,y1);
title('Subplot 1: sin(x)')

subplot(2,2,2);
plot(x,y2);
title('Subplot 2: sin(2x)')

subplot(2,2,3)
plot(x,y3);
title('Subplot 3: sin(4x)')

subplot(2,2,4)
plot(x,y4);
title('Subplot 4: sin(8x)')
```

### Subplots with Different Sizes

Create a figure with subplots of different sizes.

Set up a vector of sine values and a vector of polynomial values.

```x = linspace(-3.8,3.8);
y_cos = cos(x);
y_poly = 1 - x.^2./2 + x.^4./24;```

Plot the sine wave in the first subplot and the polynomial in the second subplot. Create a third subplot that spans the lower half of the figure and plot both vectors together. Add titles to each subplot.

```figure
subplot(2,2,1);
plot(x,y_cos);
title('Subplot 1: Cosine')

subplot(2,2,2);
plot(x,y_poly,'g');
title('Subplot 2: Polynomial')

subplot(2,2,[3,4]);
plot(x,y_cos,'b',x,y_poly,'g');
title('Subplot 3 and 4: Both')```

### Replacing Subplots

Replace a subplot with an empty axes.

Initialize the random number generator. Use a loop to create a figure with four stem plots of random data.

```rng(0,'twister');
figure
for k = 1:4
data = rand(1,10);
subplot(2, 2, k)
stem(data);
end
```

Replace the second subplot with an empty axes.

`subplot(2,2,2,'replace')`

### Subplots at Specified Positions

Specify position vectors to create two subplots that are not aligned.

Store the data from the magic function in vector y.

`y = magic(4);`

Plot the vector in one subplot. Create a bar graph in a second subplot.

```y = magic(4);
figure
positionVector1 = [0.1, 0.2, 0.3, 0.3];
subplot('Position',positionVector1)
plot(y)

positionVector2 = [0.5, 0.1, 0.4, 0.7];
subplot('Position',positionVector2)
bar(y)```

### Return Subplot Axes Handle

Create a figure with two subplots and store the handle to each subplot.

```figure
h1 = subplot(2,1,1);
h2 = subplot(2,1,2);```

Store the data from the peaks function in matrix Z. Use the axes handle to plot the first 20 rows of Z in the upper subplot. Plot the entire data set in the lower subplot.

```Z = peaks;
plot(h1,Z(1:20,:))
plot(h2,Z);```

Change the tick marks for the lower subplot using axes properties. Execute plotting functions before specifying axes properties to avoid overriding existing axes property settings.

`set(h2,'XTick',[0,10,25,40,50])`

### Specify Axes Properties

Use the Parent axes property to specify the figure for a subplot.

Create two figures and store the handle to each figure.

```f1 = figure;
f2 = figure;```

Create an axes in the lower subplot of the first figure by setting the Parent axes property value to f1. Save the handle to the subplot axes as h.

`h = subplot(2,1,2,'Parent',f1);`

Set up a vector of random data and create a stem plot. Use axes handle h to specify the axes for the plotting function.

```rng(0,'twister');
data = rand(1,10);
stem(h,data)
title(h,'Lower Subplot of First Figure')```

### Make Axes the Current Axes

Use a subplot axes handle to make it the current axes.

Create a figure with multiple subplots. Store the handle to each subplot in vector h.

```figure;
for k = 1:4
h(k) = subplot(2,2,k);
end
```

Make the second subplot the current axes. Plot a sine wave and change the axis limits.

```x = linspace(1,50);
y = sin(x);

subplot(h(2));
plot(x,y,'Color',[0.1, 0.5, 0.1]);
title('Second Subplot')
axis([0,50,-1,1])```

## Input Arguments

expand all

### m — Number of grid rows1 (default) | positive integer

Number of grid rows, specified as a positive integer.

Data Types: single | double

### n — Number of grid columns1 (default) | positive integer

Number of grid columns, specified as a positive integer.

Data Types: single | double

### p — Grid position for new axespositive integer | vector

Grid position for the new axes, specified as a positive integer or a vector of positive integers.

• If p is a positive integer, then subplot creates a new axes in grid location p.

• If p is a vector of positive integers, then subplot creates a new axes that spans the grid locations listed in p. For example, subplot(2,3,[2,5]) creates one axes spanning positions 2 and 5. Use subplot(2,3,[2,6]) to create one axes spanning positions 2, 3, 5, and 6.

Data Types: single | double

### positionVector — Normalized position for new axesfour-element vector of values between 0.0 and 1.0

Normalized position for the new axes, specified as a four-element vector of values between 0.0 and 1.0 with the form [left,bottom,width,height]. The values are normalized with respect to the interior of the figure. The first two elements specify the position of the bottom-left corner of the axes in relation to the bottom-left corner of the figure. The last two elements specify the width and height of the new axes. Use this syntax to position an axes that does not align with grid positions.

Example: [0.1, 0.1, 0.35, 0.35]

Data Types: single | double

### Name-Value Pair Arguments

Specify optional comma-separated pairs of Name,Value arguments. Name is the argument name and Value is the corresponding value. Name must appear inside single quotes (' '). You can specify several name and value pair arguments in any order as Name1,Value1,...,NameN,ValueN.

Example: 'XGrid','on'

You can set any axes properties for a subplot. Some plotting functions override existing axes property settings. Execute plotting functions before specifying axes properties to avoid overriding them. For a list of axes properties see Axes Properties.

## Output Arguments

expand all

### h — Handle to axes objectscalar

Handle to axes object, returned as a scalar. This is a unique identifier, which you can use to query and modify the properties of a specific axes object.

expand all

### Tips

• If the axes already exists, then the command subplot(m,n,p) makes the subplot in position p the current axes.

• subplot(111) is an exception and not identical in behavior to subplot(1,1,1). For reasons of backwards compatibility, subplot(111) is a special case of subplot that does not immediately create an axes, but sets up the figure so that the next graphics command executes clf reset. The next graphics command deletes all the figure children and creates a new axes in the default position. subplot(111) does not return a handle and an error occurs if code specifies a return argument.

• If a new subplot axes overlaps an existing axes, then MATLAB deletes the existing axes. To overlay a new axes on top of existing subplots, use the axes command. For example, subplot('Position',[.35 .35 .3 .3]) deletes any underlying subplots, but axes('Position',[.35 .35 .3 .3]) positions a new axes in the middle of the figure without deleting any underlying axes.

• When using a script to create subplots, MATLAB does not finalize the Position property value until either a drawnow command is issued or MATLAB returns to await a user command. The Position property value for a subplot is subject to change until the script either refreshes the plot or exits.