DISCOVER

# How to Plot Multiple Graphs on a Single Graph in Matlab

Updated July 20, 2017

Matlab is a computing environment created by the software company Mathworks. It is commonly used in many scientific and engineering applications due to its numerical computing and programming functions. One task that is important for these types of applications are plots and graphs, which can be used to understand and illuminate the results of a specific program within Matlab. Placing multiple graphs within a single graph is one way to compare different results in Matlab. This is done using the "subplot" function.

Create, import or initialise the data that you will be plotting. For example, the commands below create a data set that will eventually be used for the horizontal axis "x", and three sets of data used for the vertical axis "y.'

x = [1 2 3 4 5];

y = [2 5 8 2 1; 4 8 11 5 5; 5 9 10 0 4 ];

Use the subplot command to allocate space to place the multiple graphs. The command > subplot(3,1,1) "calls" the first subplot, allowing you to plot the data within that first plot. The first number in the subplot command indicates that there are three plots, placed vertically on top of each other; the second number indicates there is only one column of plots, and the third number indicates that the plot on the top is the one to focus on. Alternatively, the command >subplot(2,2,1) would indicate a 2 x 2 square of 4 different plots

Plot the data within the subplot. The command >plot(x,y(1,:) ) will plot the first row of data on the first plot.

Repeat the above commands to plot the remaining two graphs. In this example, these commands would be:

subplot(3,1,2)

plot(x,y(2,:) )

subplot(3,1,3)

plot(x,y(3,:) ).

There should now be three different graphs all plotted on top of one another on the same graph.

#### Tip

Use the Matlab documentation to study the correct usage of different commands, such as "plot and "subplot," and to understand how to initialise matrices in Matlab.

#### Things You'll Need

• Computer running Matlab software