The Plot Function

The plot function in Matlab is used to create a graphical representation of some data. It is often very easy to "see" a trend in data when plotted, and very difficult when just looking at the raw numbers.

The Plot Function

The plot function usually takes two arguments (but can take one). The first is the X values of the points to plot, and the second is the Y value of the points to plot. Below is an example of creating and plotting the values of the X squared graph from -10 to +10. In the case of a single argument, the X axis becomes 1,2,3,4,... up to the length of the array, and the Y axis contains the values of the array.

	% Create/Compute two sets of data
	for i=1:20
	  x(i) = i-10;
	  y(i) = x(i) ^ 2;

	% Plot the X values vs. the Y values

	% Label the plot
	xlabel('X axis');
	ylabel('Y axis');
	title('Graph of X^2');
	legend ('X squared');
	grid on;

Notice that plot will "connect the dots" for you, drawing lines between each set of X,Y values.

Single Argument to Plot

If you have a single array that you wish to plot, and don't really care about the X axis, you just want to see the values plotted in the Y axis, you can pass a single array to the plot function. Here is an example:

        grades = [90, 100, 80, 70, 75, 88, 98, 78, 86, 95, 100, 92, 29, 50];

        plot( grades );               % Plot the grades
        xlabel('Students');           % As always, label your graph
        ylabel('Grades for Quiz 1');
        title('Student Grades');

Labeling your Plot

The following functions are almost always used with plot to make the output more readable.

  1. xlabel, ylabel : Provide a labeling for the graph axii.
  2. title : Provide a title for the graph.
  3. legend: Provide a legend telling what multiple graph lines mean.
  4. grid: Put a checkered grid over the graph (add more lines to (sometimes) make it easier to see values.
  5. axis: Tell how big of an area the graph should display.

The "clf" command

Sometimes you will want to "clear" the figure. This is normally done by simply "plotting" the next thing you want. Alternatively, you can use the "clf" command:

	>> clf; % clear the plot

The "hold" command

Sometimes you will want to plot multiple graphs on the XY axis (on the same figure). In Matlab, when you use the plot function more than once, each time you call it, the previous figure is "erased".

To make a previous figure remain on the plot, we use the "hold on;" command. When we want to resume clearing the figure for each new plot, we use the "hold off;" command.

Try the following code:

	% Create some values to plot
	for i=1:20
	  x(i) = i-10;
	  squared(i) = x(i) ^ 2;
	  cube(i) = x(i) ^ 3;

	% Plot the X values vs. the Y values

You should have only seen one graph, the cubed graph (which was the second plot above). Now try this code, using the "hold on;" command.

	% Create some values to plot
	for i=1:20
	  x(i) = i-10;
	  squared(i) = x(i) ^ 2;
	  cube(i) = x(i) ^ 3;

	% Clear the figure and then set "hold" to on
	hold on;

	% Plot the X values vs. the Y values

	% set "hold" to off, so the next plot will behave normally
	hold off;

The "subplot" command

Sometimes you will want to place multiple plots side by side on a single figure. You can achieve this by using the Matlab subplot function.

For many more details on this function (as well as the commands and functions mentioned above) you can always use the help command at the Matlab prompt.

The parameters used by the subplot function determine how many rows and columns for the overall "matrix" of figures, and then which of these positions to put the next plot in. The parameters are: 1) The number of rows, 2) the number of columns, 3) which of the sub plots to use.

Here is an example of how to create a 2x3 matrix (6 figures) of plots and to address each one of them by a number (shown below). Notice how row 1, column 1, is identified by the number 1. Row one, column three is identified by 3. Row two, column one, "wraps" and is identified by 4.:


Test this code. Notice that the subplot command stays the same, except for the "index" into which matrix location you want. Also notice that you can title, label, etc, each plot.

	% Create some values to plot
	for i=1:20
	  x(i) = i-10;
	  squared(i) = x(i) ^ 2;
	  cube(i) = x(i) ^ 3;
	  linear(i) = x(i);
	  log_of(i) = log(x(i));
	  sqrt_of(i) = sqrt(x(i));

	% Set the "grid" for plots to 2 rows and 3 columns. place
	% the first plot at location 1.


	% place the last three plots across the second row



	% place the cube plot (for instructional purposes) at location 3
	% (the upper right corner).



Various additional plotting methods

Matlab provides several well known graphing functions, including histograms, pie charts, and stem charts.

  1. Histograms: Histograms are used to show how many values in an array of values fall within certain "bins". For example, if we have the scores of all students on a test, we may want to find out how many scores were between 90-100, 80-90, 70-80, etc. The histogram is the perfect tool for this.

  2. Pie Charts: Pie Charts are useful for showing what percentage of a whole, each element represents. For example, if we knew there were 40 hours in the week, we could use a pie chart to show how many hours were spent on study, sleep, play, etc

  3. Stem Charts: The stem chart draws a line from the bottom of the graph to the appropriate Y value for each X value. For example if the X axis represents how many times a letter was found in a document, we could use a stem plot to show the relationship between all letters, and how often they occur..

Back to Topics List