Combine Multiple Plots - MATLAB & Simulink
Combine Multiple Plots - MATLAB & Simulink
This example shows how to combine plots in the same axes using the hold function,
and how to create multiple axes in a figure using the tiledlayout function. Try This Example
x = linspace(0,10,50); Get
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
https://www.mathworks.com/help/matlab/creating_plots/combine-multiple-plots.html 1/8
4/3/24, 1:56 AM Combine Multiple Plots - MATLAB & Simulink
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 line styles based on the ColorOrder and LineStyleOrder properties of the axes.
The axes limits and tick values might adjust to accommodate new data.
x = linspace(0,10,50); Get
y1 = sin(x);
y2 = rand(50,1);
tiledlayout(2,1)
% Top plot
nexttile
plot(x,y1)
title('Plot 1')
% Bottom plot
nexttile
scatter(x,y2)
title('Plot 2')
https://www.mathworks.com/help/matlab/creating_plots/combine-multiple-plots.html 2/8
4/3/24, 1:56 AM Combine Multiple Plots - MATLAB & Simulink
x = linspace(0,10,50); Get
y1 = sin(x);
y2 = rand(50,1);
For example, create two plots and assign the axes objects to the variables ax1 and ax2. Change the axes font size and x-
axis color for the first plot. Add grid lines to the second plot.
https://www.mathworks.com/help/matlab/creating_plots/combine-multiple-plots.html 3/8
4/3/24, 1:56 AM Combine Multiple Plots - MATLAB & Simulink
x = linspace(0,10,50); Get
y1 = sin(x);
y2 = rand(50,1);
tiledlayout(2,1)
% Top plot
ax1 = nexttile;
plot(ax1,x,y1)
title(ax1,'Plot 1')
ax1.FontSize = 14;
ax1.XColor = 'red';
% Bottom plot
ax2 = nexttile;
scatter(ax2,x,y2)
title(ax2,'Plot 2')
grid(ax2,'on')
https://www.mathworks.com/help/matlab/creating_plots/combine-multiple-plots.html 4/8
4/3/24, 1:56 AM Combine Multiple Plots - MATLAB & Simulink
x = linspace(0,30); Get
y1 = sin(x);
y2 = sin(x/2);
y3 = sin(x/3);
y4 = sin(x/4);
% Create plots
t = tiledlayout(2,2);
nexttile
plot(x,y1)
nexttile
plot(x,y2)
nexttile
plot(x,y3)
nexttile
plot(x,y4)
Reduce the spacing around the perimeter of the layout and around each tile by setting the Padding and TileSpacing
properties to 'compact'.
https://www.mathworks.com/help/matlab/creating_plots/combine-multiple-plots.html 5/8
4/3/24, 1:56 AM Combine Multiple Plots - MATLAB & Simulink
x1 = linspace(0,20,100); Get
y1 = sin(x1);
x2 = 3:17;
y2 = rand(1,15);
% Create plots.
t = tiledlayout(2,1);
ax1 = nexttile;
plot(ax1,x1,y1)
ax2 = nexttile;
stem(ax2,x2,y2)
https://www.mathworks.com/help/matlab/creating_plots/combine-multiple-plots.html 6/8
4/3/24, 1:56 AM Combine Multiple Plots - MATLAB & Simulink
Add a shared title and shared axis labels by passing t to the title, xlabel, and ylabel functions. Move the plots closer
together by removing the x-axis tick labels from the top plot and setting the TileSpacing property of t to 'compact'.
https://www.mathworks.com/help/matlab/creating_plots/combine-multiple-plots.html 7/8
4/3/24, 1:56 AM Combine Multiple Plots - MATLAB & Simulink
See Also
Functions
tiledlayout | nexttile | title | hold
Related Topics
Create Chart with Two y-Axes
Specify Axis Tick Values and Labels
MATLAB® provides a variety of ways to write unit tests so you can start quickly and build up as needed.
Learn more
https://www.mathworks.com/help/matlab/creating_plots/combine-multiple-plots.html 8/8