Plots with More than One Line

A plot with more than one line can be created in several ways. By default, the execution of a second plot tatement will erase the first plot. However, you can layer plots on top of one another by using the hold on command. Execute the following statements to create a plot with both functions plotted on the same graph, as shown in Figure 5.4 :

**x = 0:pi/100:2*pi;
**

**y1 = cos(x*4);
**

**plot(x,y1)
**

**y2 = sin(x);
**

**hold on;
**

**plot(x, y2)
**

Semicolons are optional on both the plot statement and the hold on state-ment. MATLAB ® will continue to layer the plots until the hold off command is executed:

**hold off
**

Another way to create a graph with multiple lines is to request both lines in a single plot command. MATLAB ®interprets the input to plot as alternating x and y vec-tors, as in

**plot(X1, Y1, X2, Y2)
**

where the variables X1 , Y1 form an ordered set of values to be plotted and X2 , Y2 form a second ordered set of values. Using the data from the previous example,

**plot(x, y1, x, y2)
**

produces the same graph as Figure 5.4 , with one exception: The two lines are differ-ent colors. MATLAB ® uses a default plotting color (blue) for the first line drawn in a plot command. In the hold on approach, each line is drawn in a separate plot command and thus is the same color. By requesting two lines in a single command, such as plot(x,y1,x,y2) , the second line defaults to green, allowing the user to

distinguish between the two plots.

If the plot function is called with a single matrix argument, MATLAB ®draws a separate line for each column of the matrix. The x -axis is labeled with the row index vector, 1: k , where k is the number of rows in the matrix. This produces an evenly spaced plot, sometimes called a line plot. If plot is called with two argu-ments, one a vector and the other a matrix, MATLAB ®successively plots a line for each row in the matrix. For example, we can combine y1 and y2 into a single matrix and plot against x :

**Y = [y1; y2];
**

**plot(x,Y)
**

This creates the same plot as Figure 5.4 , with each line a different color. Here’s another more complicated example:

**X = 0:pi/100:2*pi;
**

**Y1 = cos(X)*2;
**

**Y2 = cos(X)*3;
**

**Y3 = cos(X)*4;
**

**Y4 = cos(X)*5;
**

**Z = [Y1; Y2; Y3; Y4];
**

**plot(X, Y1, X, Y2, X, Y3, X, Y4)
**

This code produces the same result ( Figure 5.5 ) as

**plot(X, Z)
**

A function of two variables, the peaks function produces sample data that are useful for demonstrating certain graphing functions. (The data are created by scal-ing and translating Gaussian distributions.) Calling peaks with a single argument n

will create an nn matrix. We can use peaks to demonstrate the power of using a matrix argument in the plot function. The command

**plot(peaks(100))
**

results in the impressive graph in Figure 5.6 . The input to the plot function created by peaks is a 100X100 matrix. Notice that the x -axis goes from 1 to 100, the index numbers of the data. You undoubtedly can’t tell, but there are 100 lines drawn to create this graph—one for each column.