How to Plot Multiple Plots in MATLAB

Ammar Ali Feb 02, 2024
How to Plot Multiple Plots in MATLAB

This tutorial will discuss plotting multiple plots using the figure command in Matlab.

Plot Multiple Plots Using the figure Command in MATLAB

In Matlab, if we plot a variable and after that, we plot another variable, the second variable will overwrite the first variable. To solve this problem, we have to use the figure command. The figure command is used to initialize a figure. For example, if we want to plot two variables on two different figures. We have to use the figure command before we plot the variable. Let’s plot two graphs on two different figures using the figure command. See the code below.

t = -1:0.1:1;
x = sin(2*pi*t);
y = cos(2*pi*t);
figure
plot(x)
figure
plot(y)

Output:

Plot two separate plots in Matlab

There are two figures, Figure1 and Figure2 in the output, but there will only be one figure with one plot if we don’t use the figure command. You can also give a title name to each figure using the Name property of the figure command.

We can also set other properties like the figure’s position and size using the Position property of the figure command. If we want to plot multiple plots in the same figure, we can use the subplot() function. To use the subplot() function, we first have to define the number of rows and columns in the figure.

Let’s define two by two grid that means the plot will have two rows and two columns that mean the figure will contain four plots. Every time we plot a variable, we have to use the subplot command and define the position of the plot as the third argument. If we want the plot the variable at the first position, we need to give the third argument an integer 1. For example, Let’s plot the above two graphs in the same figure using the subplot() function. See the code below.

t = -1:0.1:1;
x = sin(2*pi*t);
y = cos(2*pi*t);
figure
subplot(1,2,1)
plot(x)
subplot(1,2,2)
plot(y)

Output:

Ploting Graphs on the Same Figure in Matlab

In the output, there are two graphs in the same figure. We can also give each plot a title using the title() function.

Author: Ammar Ali
Ammar Ali avatar Ammar Ali avatar

Hello! I am Ammar Ali, a programmer here to learn from experience, people, and docs, and create interesting and useful programming content. I mostly create content about Python, Matlab, and Microcontrollers like Arduino and PIC.

LinkedIn Facebook

Related Article - MATLAB Plot