Student Center
Visualizing Data
- Prerequisites and Expectations for the Tutorial
- A Very Elementary MATLAB Tutorial
- Starting MATLAB
- Running the Demos
- Getting Help
- Creating Variables
- Performing Calculations
- Visualizing Data
- Creating Scripts with MATLAB Editor/Debugger
- Saving Variables and Sessions
- Working with Files, Directories and Paths
Introduction
In the previous lesson, you learned how to 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 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]simple_x_points = 1 2 3 4 5
simple_y_points = [25 0 20 5 15]simple_y_points = 25 0 20 5 15Then, to plot these vectors in MATLAB you would type the following at the command prompt:
plot(simple_x_points, simple_y_points);MATLAB will pop-up a figure window containing the line plot; it will look like this.
![]() |
| Click on image to see enlarged view. |
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:
plot(simple_y_points, simple_x_points);then, MATLAB would pop-up a figure window with this plot.
![]() |
| Click on image to see enlarged view. |
Note: You can easily print out the line plot by selecting File->Print on the menu at the top of the figure window.
Besides just the basic line plot, MATLAB has commands which will let you add titles and labels to your figures. To add text to a plot, you need to keep the figure window open (i.e., type the commands in the MATLAB command window while the figure 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 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 figure window (typing "grid off" 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');
ylabel('this is text describing the y-axis');
title('this is text giving a title for the graph');
grid on;And, here is how these commands would affect the first plot.
![]() |
| Click on image to see enlarged view. |
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];
y_points = x_points .^ 2;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 Parabola');
grid on
![]() |
| Click on image to see enlarged view. |
Note: 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.
Multiple Plots and Subplots
Another thing you might want to do is superimpose multiple plots in the same figure window, to compare the plots for example. This can be done using the hold command. Normally, when you type a plot command, any previous figure window is simply erased, and replaced by the
results of the new plot. However, if you type "hold on" at the command prompt, all line plots created after that will be superimposed in the same figure window and axes. Like wise the command "hold off" will stop this behavior, and revert to the default
(i.e., new plot will replace the previous plot).
Here is and example of how you can plot several different exponential functions in the same axes (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):
x_points = [-10 : .05 : 10];
plot(x_points, exp(x_points));
grid on
hold on
plot(x_points, exp(.95 .* x_points));
plot(x_points, exp(.85 .* x_points));
plot(x_points, exp(.75 .* x_points));
xlabel('x-axis'); ylabel('y-axis');
title('Comparing Exponential Functions');
![]() |
| Click on image to see enlarged view. |
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). 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).
Below is an example of a line plot, a parabola, an exponential, and the absolute value function into four rectangles in the same figure window.
x_points = [-10 : .05 : 10];
line = 5 .* x_points;
parabola = x_points .^ 2;
exponential = exp(x_points);
absolute_value = abs(x_points);
subplot(2,2,1);plot(x_points,line);
title('Here is the line');
subplot(2,2,2);plot(x_points,parabola);
title('Here is the parabola');
subplot(2,2,3);plot(x_points,exponential);
title('Here is the exponential');
subplot(2,2,4);plot(x_points,absolute_value);
title('Here is the absolute value');![]() |
| Click on image to see enlarged view. |
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, 1) three-dimensional line plots and 2) surface mesh plots.
Let's start with three-dimensional line plots, which are analagous to the two-dimensional line plots created with the plot command. The only difference is that the command has a "3" added to it, plot3, and that it requires an extra input, Z, for the third dimension.
Here is a simple example of using the plot3 command, and the resulting output figure 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];
Y = [10 20 30 40];
Z = [0 210 70 500];
plot3(X,Y,Z); grid on;
xlabel('x-axis'); ylabel('y-axis'); zlabel('z-axis');
title('Pretty simple');
![]() |
| Click on image to see enlarged view. |
As you can see, 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.
Z = [0 : pi/50 : 10*pi];
X = exp(-.2.*Z).*cos(Z);
Y = exp(-.2.*Z).*sin(Z);
plot3(X,Y,Z); grid on;
xlabel('x-axis'); ylabel('y-axis'); zlabel('z-axis');...
title('A little more interesting!');Note: ellipses --- if you type "..." at the end of a command line, you can hit return and then continue typing the command on the next line).
![]() |
| Click on image to see enlarged view. |
Three-Dimensional Surface Mesh Plots
You can use the mesh and meshgrid commands to create surface mesh plots, which show the surface of three-dimensional functions, such as "z = x^2 + y^2". The way it works is that you:
1) Generate a grid of points in the xy-plane using the meshgrid command.
2) Evaluate the three-dimensional function at these points.
3) 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];
y_points = [-10 : 4 : 10];
[X, Y] = meshgrid(x_points,y_points);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.
mesh(X,Y,Z); xlabel('x-axis'); ...
ylabel('y-axis');zlabel('z-axis');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 image to see enlarged view. |
Continue on to the next lesson.
See also: Simulink Tutorial
Store








