This is machine translation

Translated by Microsoft
Mouseover text to see original. Click the button below to return to the English version of the page.

Note: This page has been translated by MathWorks. Click here to see
To view all translated materials including this page, select Country from the country navigator on the bottom of this page.

Combine Multiple Plots

You can combine plots in several ways. Combine plots in the same axes, or create multiple axes in a figure using subplots.

Combine Plots in Same Axes

By default, new plots clear existing plots and reset axes properties, such as the title. However, you can use the hold on command to combine multiple plots in the same axes. For example, plot two lines and a scatter plot. Then reset the hold state to off.

x = linspace(0,10,50);
y1 = sin(x);
plot(x,y1)
title('Combine Plots')

hold on

y2 = sin(x/2);
plot(x,y2)

y3 = 2*sin(x);
scatter(x,y3) 

hold off

When the hold state is on, new plots do not clear existing plots or reset axes properties, such as the title or axis labels. The plots cycle through colors and lines styles based on the ColorOrder and LineStyleOrder properties of the axes. The axes limits and tick values might adjust to accommodate new data.

Create Multiple Axes in Figure Using Subplots

Create multiple axes in a single figure using the subplot function, which divides the figure into a grid of subplots. The first two inputs to subplot indicate the number of subplots in each row and column. The third input specifies which subplot is active. MATLAB numbers subplot positions by row. Graphics functions, such as plot and title, target the active subplot.

For example, create two stacked subplots in a 2-by-1 grid within a figure window. Add a title to each subplot.

subplot(2,1,1);
x = linspace(0,10,50);
y1 = sin(x);
plot(x,y1)
title('Subplot 1')

subplot(2,1,2);
y2 = rand(50,1);
scatter(x,y2)
title('Subplot 2')

Create Subplot that Spans Multiple Grid Positions

To create a subplot that spans multiple grid positions, specify the third input argument to the subplot function as an array of positions. For example, create a 2-by-2 grid of subplots. Create two subplots in the top half of the figure and one subplot that spans the entire bottom half.

figure
subplot(2,2,1);
x = linspace(0,10,50);
y1 = sin(x);
plot(x,y1)

subplot(2,2,2);
y2 = rand(50,1);
scatter(x,y2)

subplot(2,2,[3 4]);
y3 = rand(50,1);
plot(x,y2)

Modify Subplot Appearance

Modify the subplot appearance by setting properties of the underlying Axes object. Access the Axes object using the output argument from the subplot function. You also can use the Axes objects as the first input argument to graphics functions to ensure that the function targets the correct subplot.

For example, create two subplots and assign the Axes objects to the variables ax1 and ax2. Change the axes font size and x-axis color for the first subplot. Add grid lines to the second subplot.

figure
ax1 = subplot(2,1,1);
x = linspace(0,10,50);
y1 = sin(x);
plot(ax1,x,y1)
title(ax1,'Subplot 1')
ax1.FontSize = 14;
ax1.XColor = 'red';

ax2 = subplot(2,1,2);
y2 = rand(50,1);
scatter(ax2,x,y2)
title(ax2,'Subplot 2')
grid(ax2,'on')

Add Super Title to Grid of Subplots

When you create a figure with subplots, you might want to add a title that applies to all the subplots. Starting in R2018b, you can use the sgtitle function to add a title to a grid of subplots.

subplot(2,1,1);
x = linspace(0,10,50);
y1 = sin(x);
plot(x,y1)
title('Subplot 1')

subplot(2,1,2);
y2 = rand(50,1);
scatter(x,y2)
title('Subplot 2')

sgtitle('My Subplot Grid Title')

See Also

Functions

Related Topics