In the previous lesson, you learned how you could create a vector of regularly spaced points, and another vector containing the values of a function at those regularly spaced points. In this lesson, you will learn how you can use such vectors to do basic plotting in two dimensions and three dimensions. MATLAB has many advanced plotting features and you can create a great variety of graphs and charts with it. This lesson will only cover the basic aspects of plotting; the core lessons will go into some more detail, and you should also explore the MATLAB help texts for more advanced information about plotting in MATLAB.
Line Plots in Two Dimensions
The most basic plotting command in MATLAB is, surprise, surprise, the "plot" command. The "plot" command, when called with two same-sized vectors X and Y, makes a two-dimensional line plot where a point is made on the plot for each point in X and its corresponding point in Y: the numbers in X are on the abscissa (x-axis), and the numbers in Y are on the ordinate (y-axis). In other words, it will draw points at (X(1),Y(1)), (X(2),Y(2)), (X(3),Y(3)), etc., and then connect all these points together with lines. Let's do a very simple example just to show you exactly what the "plot" command does. So, let's create two simple vectors in MATLAB:
simple_x_points = [1 2 3 4 5]
Then, to plot these vectors in MATLAB you would type the following at the command prompt:
And, after hitting return MATLAB will pop up another window containing the line plot; it will look like this:
So, as you can see, it plotted points at (1,25), (2,0), (3,20), (4,5), and (5,15) --- i.e., the points gotten from the two vectors "simple_x_points" and "simple_y_points" --- and drew lines between these points. The ordering of the vectors in the "plot" command is important, so, for example, if we reversed the order from our simple example:
then, MATLAB would pop up a window with this plot:
You can print out the line plot by selecting "Print" from the "File" menu at the top of the window containing the line plot (and, if you are done working with a plot, you can close its window by clicking the mouse on the X in the window's top right corner). However, if you print out a lot of plots like this, you might get confused as to what each plot represents. It would be nice if you could add some text to the plots describing such things as what the plot represents, and what the units of the x and y axes are, and what they represent. Well, MATLAB has commands which will let you do these things. To run these commands to add text to a plot, you should keep the plot window open (i.e., type the commands in the MATLAB command window while the plot window is still open).
The "xlabel" command allows you to print out a text string describing the x-axis; similarly, the "ylabel" command allows you to print out a text string describing the y-axis. And, the "title" command allows you to print out a text string giving a title for your plot. Finally, to add grid lines to your plot, you can type "grid on" at the command prompt, and grid lines will be added to the open plot window (typing "grid off" later, with the plot window still open, will get rid of the grid lines). Here are examples, showing you how to use these commands:
xlabel('this is text describing the x-axis');
And, here is how these commands would affect the first plot window above if it were open when the commands were typed:
Let's look at a more practical example of plotting. So, first recall how we could create a vector of regularly spaced points and a vector of function values at those points for some function. Here is how we could do this for the function "y = x^2" (i.e., a parabola) for x values between -5 and 5 and with regular spacing of .1:
» x_points = [-5 : .1 : 5];
It is then a simple matter to plot the x_points against the y_points, and get the familiar plot of a parabola:
plot(x_points,y_points); xlabel('x-axis'); ylabel('y-axis'); title('A
Notice that the result is very smooth: you can't really see any of the individual line segments like you could for the simple example previously. That is because the points are so close together (at regular spacings of .1) --- MATLAB is still drawing line segments between the points, but your eye just can't see them because they are so small, and so the result seems to be a smooth curve.
Another thing you might want to do is have multiple plots in the same window, for example to be able to compare the plots more easily. You can do this using the "hold" command. Normally, when you type a "plot" command, any previous plot window is simply erased, and replaced by the results of the new "plot" command. However, if you type "hold on" at the command prompt, all "plot" commands typed after that will be directed to the same window (to the currently opened plot window, if you previously executed a "plot" command); typing "hold off" will stop this behavior, and revert to the default (i.e., new "plot" commands cause previous plot windows to be replaced). For example, here is how you could plot several different exponential functions in the same plot window, and the resulting plot window (notice how you don't necessarily have to define separate variables for the different functions, but can merely insert the expressions for them directly into the plot command):
= [-10 : .05 : 10];
Still another thing you might want to do is to have multiple plots in the same window, but each in a separate part of the window (i.e., each with their own axes and grid lines, etc.). You can do this using the "subplot" command. If you type "subplot(M,N,P)" at the command prompt, MATLAB will divide the plot window into a bunch of rectangles --- there will be M rows and N columns of rectangles --- and MATLAB will place the result of the next "plot" command in the Pth rectangle (where the first rectangle is in the upper left). For example, here is how you could plot a line, a parabola, an exponential, and the absolute value function into four rectangles in the plot window:
x_points = [-10 : .05 : 10];
And, here is the plot window you would get for this:
Line Plots in Three Dimensions
Well, that about covers basic plotting in two dimensions. Let's move on to how you can create basic plots in three dimensions. Here we'll cover two different kinds of three-dimensional plots you can do in MATLAB, three-dimensional line plots and surface mesh plots. So let's start with three-dimensional line plots, which are analogous to the two-dimensional line plots created with the "plot" command. Three-dimensional line plots are created with the "plot3" command. The "plot3" command, when called with three same-sized vectors X, Y, and Z, makes a three-dimensional line plot where a point is made on the plot for each point in corresponding point in X, Y, and Z: the numbers in X are on the x-axis, the numbers in Y are on the y-axis, and the numbers in Z are on the z-axis. In other words, the "plot3" command will draw points at (X(1),Y(1),Z(1)), (X(2),Y(2),Z(2)), (X(3),Y(3),Z(3)), etc., and then connect all these points together with lines in three-dimensional space. Here is a simple example of using the "plot3" command, and the resulting output plot window (notice how the all the text labeling commands, including an additional "zlabel" command, and the "grid" command can be used just as in the two-dimensional "plot" command --- you can also use "hold" and "subplot" in the same way too):
» X = [10
20 30 40];
As you can see (although it is a little harder to see in three dimensions than in two), MATLAB plotted points at (10,10,0), (20,20,210), (30,30,70), and (40,40,500) --- i.e., the points gotten from the three vectors "X", "Y", and "Z" --- and drew lines between these points. Here is a more interesting, but complex, example (notice the use of "..." --- if you type "..." at the end of a command line, you can hit return and then continue typing the command on the next line):
» Z = [0
: pi/50 : 10*pi];
And here is the plot you get after typing this:
Three-Dimensional Surface Mesh Plots
You can use the "mesh" and "meshgrid" commands to create surface mesh plots, which are three-dimensional plots which show the surface of three-dimensional functions, such as "z = x^2 + y^2". The way it works is that you first generate a grid of points in the xy plane using the "meshgrid" command, then you evaluate the three-dimensional function at these points, and finally you create the surface plot with the "mesh" command. To generate the grid of points, you make vectors of regularly spaced points for the X and Y axes, then you call the "meshgrid" command on these vectors to create the grid of points (don't worry about the details, but "meshgrid" basically returns all possible combinations of (x,y) points, where x is taken from X and y is taken from Y, in the form of two matrices --- so what you will see in the generated surface mesh plot will be a bunch of rectangles, of width and length equal to the regular spacing values for X and Y, and the height of the corners of the rectangles will be equal to the value of the function at the rectangles' corner points). For example:
» x_points = [-10 : 1 : 10];
Then, you evaluate the function at the grid points. Here is an example, using the function "z = x^2 + y^2" (i.e., a three-dimensional parabola):
» Z =
X.^2 + Y.^2;
Finally, you create the surface mesh plot, and add any labels you might want:
Here is the resulting plot (notice how the rectangles are of length 4 in the y-axis direction, and 1 in the x-axis direction --- this is because we chose spacing 4 for Y and 1 for X):
Click on the following MATLAB command names to view their help text.
To go on to core lesson 1, click here.