Skip to main content

Section 11.2 Multiple Plots

A lot of the time it is useful to show someone multiple plots. Perhaps you want to compare two materials or show different projections. MATLAB has the capability to plot multiple plots on the same axes or display multiple plots in the same figure. See figure 11.2.1 below for an example.
Figure 11.2.1. Examples of the different kinds of multiple plotting you can accomplish in MATLAB.
You should have also noticed that the plots in Figure 11.2.1 above have titles, axes labels, and legends (where appropriate). In the next section of this chapter, we will learn how to accomplish this. For now, let’s concentrate on what we need to learn to do either:
  1. Create two plots on the same axes.
  2. Create multiple plots in the same figure window.
Try It!
Before we jump into learning those two points, you should have noticed that I added tensile testing data on Teflon to the charts. To follow along, you will need to do so as well. Go ahead and add a new array called \(len_teflon\) to your script with the following values:
len_teflon = [25 28.2 31.4 34.5 37.8 40.8 44.1 47.3 50.5]/1000;
We will consider the case where the Teflon specimen has the exact same starting dimensions as the HDPE specimen we are using before. We will also test the Teflon under the exact same engineering stresses. With that being said, calculate the engineering stress in an array called e_stress_teflon and the engineering strain in an array called e_strain_teflon. Do not skip ahead to see my solution UNTIL you have tried it yourself!

Example 11.2.2. Stress and Strain Calculation.

Here is an interactive code box that functions identically to the MATLAB command window.
To calculate the engineering stress and engineering strain in Teflon, I added the following 2 lines to my script:
e_stress_teflon = force / area;
e_strain_teflon = (len_teflon-len_0)/len_0;
Create Multiple Plots on the Same Axes
Now that we have two sets of data, we can practice plotting them on the same axes. To do so, you just need to pretend you are a cowboy, and that MATLAB is a horse. So we just need to tell MATLAB β€œto hold on there partner, I want to plot multiple plots on the same axes”.
Figure 11.2.3. Woah there, partner. Hold on.
I wasn’t kidding! You really just have to add the line hold on to your script! For example, if we wanted to plot the HDPE and Teflon tensile test data on the same axes, we can just type in:

Example 11.2.4. Updated Example Code.

Here is an interactive code box that functions identically to the MATLAB command window.
The first line plots the HDPE data as we have done before (make sure you know what 'mp--' does!). The second line (hold on) tells MATLAB to wait and plot subsequent plots on the same axes. After you turn hold on, MATLAB will put ALL plots on those axes. Therefore, it is a good idea to turn hold off when you are done in case you need to plot other things, not on the same axes.
Go ahead and try it in your script! Notice how it plots the two plots on the same graph!
Creating Multiple Plots in the Same Window
In this particular example (tensile test data of HDPE and Teflon) it makes the most sense to plot the graphs on the same axes. But for the sake of learning, let’s go ahead and create a new figure window and create multiple plots in the same figure window. To do so, we will need to use the subplot() function.
Figure 11.2.5. Example of how to use the subplot() function and what m, n, and p should be.
First, take a second and type in help subplot into the command window and read the help text.
The trick with a subplot is to follow the following steps before you start coding:
  1. Think about how many plots you need. For this thought experiment, let’s say we need 6 plots.
  2. Then think about how you want your plots arranged. It really helps to sketch it out on a little piece of paper (kind of like in figure 11.2.5) to get a good idea. Let’s say for this example we want 2 rows and 3 columns of plots.
  3. After you think, you are ready to use the subplot() function.
The subplot(m,n,p) function divides the figure window into mxn rectangular plots. The p indicates the number of the subplot. The subplots are numbered starting at 1 in the upper left corner and increasing in number from left to right. Figure 11.2.5 is illustrating this. For the plot in the upper left corner, the user would type in:
subplot(2,3,1)
[insert plot command here]
Then if the user wanted to plot the next one to the left they would just continue…
subplot(2,3,2)
[insert plot command here]
And so on and so forth.
Figure 11.2.6. Example of 6 plots in the same figure using the subplot() command.
Try It!
Go ahead and try creating a 2x3 subplot grid with our tensile test data. You can reuse the data, so this IS a contrived example after all! But it is good practice.
Figure 11.2.7. My sketch of how I wanted my subplots to look.
I suggest sketching out your plan. Change the colors between each subplot and write them out on paper BEFORE you code to make sure you understand what is going to happen.
It is ok to struggle a little bit here! Don’t jump straight to the solution until you try it! Subplots are kind of complicated and not straightforward but you need to practice.

Checkpoint 11.2.8. Subplot Position.

What is the correct subplot command to plot a graph in the 3rd column of the 2nd row of a six-subplot figure?
  • subplot(2,3,6)
  • Correct. A 2-by-3 subplot layout has 2 rows and 3 columns. MATLAB numbers the subplots from left to right, top to bottom, making the subplot in the 3rd column of the 2nd row position 6.
  • subplot(3,2,6)
  • Incorrect. This creates a 3-by-2 subplot layout rather than a 2-by-3 layout.
  • subplot(6,2,3)
  • Incorrect. The first two arguments specify the number of rows and columns, not the total number of subplots and columns.
Subplot Solution
To create the 6 subplots shown in Figure 11.2.7 above, this is what I added to my script.

Example 11.2.9. Subplot Updated Code.

Here is an interactive code box that functions identically to the MATLAB command window.
You have attempted of activities on this page.