[embedyt] http://www.youtube.com/watch?v=AvJbKHQTI5U[/embedyt]

Click here to subscribe for more videos like this!

**Creating subplots, 3-D plots, and labels in MATLAB**

So in this lecture we are going to talk about more plotting. We’re going to talk about subplots, so that’s putting more than one plot into the same figure, we’re going to talk about three-dimensional plots, and we’re going to talk about how to label plot, so you want some field for the x-axis, the y-axis, and the title. So, let’s go into subplots. So suppose I have some

function of X, so let’s just say Y = sin(x), then I have another function which is just Z = Y + randn(1,100); Ok, so I can plot these individually, I can plot them together, I might want to stack them up or something to see if there is a correlation between them, so the way we do that is with subplot. So we call a function called subplot and it takes in three values. Ok, so the three values are the number of rows in the subplot, the number of columns in the subplot, and then which subplot you’re about to plot. So since I have two subplots let’s say I want two rows in one column and I want to set the first plot. So I just called subplot(2,1,1); and I say plot(X,Y); Ok, so now my figure has X,Y in the first row and first column. So now I do subplot(2,1,2); because I still have two rows in one column, but now I want to plot something in the second slot. Here I’m going to plot X and Z. So now if I look at my plot you can see the two plots stacked up on top of each other instead of overlaid. So the next thing we’re going to talk about is 3D plots. So suppose we have some two dimensional data set I’m just going to make it random noise. Okay, so we look at the matrix Z, we see that it’s 100 by 100, so the rows you can think of as the x-axis, the columns is the y-axis, and then the value as the third dimension. So how would we plot this? So there are a couple ways you may have seen already from your math courses, so we can plot an actual 3D plot so sort of how you would draw a cube on a two-dimensional piece of paper, or you could use what’s called a contour plot which draws lines where the value of the function is equal. So we’re going to do both of those. The first one is a function called surf, so you pass in the matrix and so you can see the x-axis and the y-axis both go from zero to a hundred. If you look up the documentation you can set the x-axis and y-axis manually, and then the height is the value of the function. So what I did just now as I clicked on the this thing that sort of looks like a circular arrow and so what that allows me to do is it allows me to rotate the surface plot, so now I can look at it from different angles which could be very useful if you’re doing data analysis. The other type of plot that we talked about is the contour plot. So the simplest way to call that is with the one argument and that generates just a 2D visualization of your data, the red is the really high values, and the blues the really low values. So the third thing we’re going to talk about today is how to label plots. So far we’ve only seen plain visualizations, so let’s go ahead and plot the sine function again. Alright, so I want to give this plot a title, all I do is use the title function title(‘A plot of sin(x)’) Ok, so now if I look at my plot you can see it now has the title A plot of sin(x). Now I want to label the x axis I’m going to label it with the very unoriginal label. Ok, so now you can see the x-axis is labeled and you can do a similar thing for the y label as well. Ok, so that’s pretty much it for labeling, suppose I want to label a subplot. Ok, so now I’m going to create another function, going to subplot(2,1,1), plot(X,Y), title(‘sin(x)’) Ok, so once I do that the first plot, first subplot gets the title sin(x). Now I call subplot(2,1,2), I plot(x,z) and I call title again so it’s the same function but it’s stateful so it knows that I called subplot earlier and that it corresponds now to the second plot. Ok, so I call title again and now my second plot has the title cos(x), and my first plot has the title sin(x).