In today’s lesson, we will be covering the following topics:
1. Overlaying Plots
2. Subplots
VIDEO COMING SOON
Overlaying Plots
We learned in a previous lesson how to plot simple X-Y graphs, but what if want to show multiple data sets in one plot? To do this, we can overlay plots.
To do this, there are two ways – we can either use only one plot command, or we can use two (or more) plot commands. Before this we need to specify all the x- and y- pairs that need to be plotted. From the previous X-Y Plotting lesson, remember that there needs to be the same number of data points plotted together (eg. X cannot have 5 points and Y has 6 – they need to have equal points so they can make (x,y) pairs).
Take a look at the two examples below. I have created the vectors X, Y1, and Y2. I plotted both X with Y1 and then X with Y2. I only used one plot command in the first image, where you type in the (X,Y) vectors you’re plotting side by side in the same command. Because I’m plotting X with Y1 then X with Y2, I used plot(X,Y1,X,Y2). In the second image, I used two plot commands because I used the words hold on before plotting, then I continued to plot both (X,Y1) then (X,Y2) separately. This stops MATLAB from overwriting the plot, and instead keeps the same one for all the (x,y) pairs you plot. I’ve also highlighted the code in yellow below for both images.
I’ve also shown the graph that comes up when the code is run. We can specify line styles/colors and add a legend to the graph, which is discussed in a future lesson (click this link).
Let’s check out another example. Here I have created vectors using linspace (click here for the link to the lesson where we discuss linspace), and I’m plotting trigonometric functions. I use one plot command to plot them all at once, but remember that you can also use the hold on command and use a separate plot command for each plot.
I’ve highlighted the code in yellow, and the resulting graph that pops up when we run it:
Subplots
Subplots are different than overlaying plots because now we are able to create multiple, smaller plots in one box, rather than overlaying all the plots on top of one another. Let’s take a look at how this works – I’ve highlighted the new subplot code I added in the example below. This is the same as one of the examples I did for overlaying plots, where I used the hold on command to overlay plots. Here I don’t need to use hold on, but I need to use a subplot command before each plot command.
How do you know what to put in the brackets next to subplot? I have subplot(2,1,1) and subplot(2,1,2). The reason I have these numbers is because subplot works with the syntax subplot(m,n,p). m is the number of rows, n is the number of columns (recall that a matrix is sometimes called an m x n matrix because it’s m rows by n columns), and p is the position you want to put that plot in (which is why each plot command needs its own separate subplot command right before). In this example below, I have 2 rows and 1 column (m = 2 and n = 1) and the first graph is in position 1, the second graph is in position 2. This was why I had subplot(2,1,1) and subplot(2,1,2). The first two numbers (in my case 2,1) stay the same in both brackets because the number of rows and columns must stay the same for all plots. The last number will change depending what position you want the plot to be in. Position 1 was the top graph, and position 2 was the bottom graph.
If there are more than just 2 graphs though, how do you know what position equates to what number? Let’s take a look at this example with 4 plots. This has 2 rows and 2 columns so m = 2 and n = 2. The last number will change for each plot – positions 1, 2, 3, or 4. The graphs always read left to right in each row. The way I have numbered them is the way that each plot is positioned when there are multiple rows and columns.