More Matlab Plotting Details


In Matlab, strings of characters are delineated by single quote right-leaning marks: '
Whenever you give a pair of vectors to Matlab to plot, you can also give a 1,2, or 3 character string that indicates the color, the marker type, and the line type. So the command
plot(x,y,'r+:') 
will plot y against x, using a red marker "+" and a red dotted line (the dotted line is indicated by the colon; how would you have found that out if I had not put in this parenthetical remark?). If you simply use
plot(x,y,'r+') 
then it will plot the data points but no line connecting them. Matlab further allows a user access to "handle graphics", the object-oriented substrata for plotting data. Using handle graphics you can change the marker type to be an alphabetic letter, for example, or a Greek symbol. As another example, when you plot multiple curves in a single plot (remember, this is called a set of "axes" by Matlab), the color of the line will by default cycle through a fixed set of colors for each new curve. Handle graphics allows you to reset that, using the ColorOrder property. Using handle graphics goes beyond what is needed in an introductory course. If you do use Matlab professionally in your student career, you want to be aware of the underlying capabilities.

I have not found any occasion to use handle graphics over the past 15 years. Before that, a little.

Plotting variants and helpers that are extremely useful in scientific computing are

The later is particularly useful when you want to save paper, and you should use it when appropriate. The legend command will put a small box inside the graph, with strings to identify the linetypes/marker types. Matlab tries by default to put it where it does not obscure much, but if you don't like the placement you can use the left mouse button to drag it where you want to place it.

Not enough info given above to really figure out how to use the commands? Well, crank up Matlab and turn to your old friend the help command!

Omitting Points in a Plot

Recall the Matlab idiom for replacing specified values in an array. A common case is where you want to remove some entries from a plot. By default, Matlab will not plot a point with a coordinate equal to Use those special values to delete points that way. Consider plotting the tangent function on [0,10*pi]:
>> x = linspace(0,4*pi,1000); 
>> y = tan(x);               
>> plot(x,y)
which yields the plot

Plot 1 of Tangent Function

[tan1.gif]

This is not helpful. The reason is the tan function has poles (goes to +/- infinity) at pi/2, 3*pi/2, .... So remove entries that are bigger than 10 in absolute value:

>> y = y - (abs(y)>10).*y;
>> plot(x,y)
giving the plot

Plot 2 of Tangent Function

[tan2.gif]

However, the plot is still unsatisfactory. It shows the graph crossing the x-axis at pi/2, etc., because Matlab connects consecutive points. Also, it puts values of zero around the asymptotes, which clearly are not valid. The trick is to eliminate those, by replacing those data values with NaN (NotaNumber):

>> y = y + (abs(y)>10)*NaN;
>> plot(x,y)
which gives the plot:

Plot 3 of Tangent Function

[tan4.gif]
Not particularly good, either. The problem here is that the "abs(y)>10" gives us a 0-1 array. But Nan times any number (including 0) is NaN, so we got an array full of NaN's. Plotting those leads to an empty plot. To fix this, we need to use the find function, which returns the indices where a relational operator is true:
>> I = find(abs(y)>10);
>> y(I) = NaN;
The vector I has the indices where y is greater than 10 in absolute value, and the second line sets only those entries to be NaN. One more try at plotting gives:

Plot 4 of Tangent Function

[tan3.gif]
Which is just lovely. Except it would be nice to put the x-axis in as a black horizontal line, and to put dotted lines in at the asymptotes, .... For practice, you might try doing all of those things.

Plotting Matrices

Although we will almost always plot vectors against vectors, a question that comes up often is how Matlab plots a matrix. For example, if you perform
>> A = eye(4)          % Set up the identity matrix of order 4
A =
     1     0     0     0
     0     1     0     0
     0     0     1     0
     0     0     0     1
>> plot(A)
Then you get the plot

Result of plot(eye(4))

[ploteye.gif]

Matlab has plotted four "curves". Each is a column of A plotted against its index number. So the first curve is the broken line through the points (1,1), (2,0), (3,0), and (4,0). You only see the part between x-coordinates 1 and 2, however, because the rest of the line lies along the x-axis. The later curves (from the remaining columns of A) overlay it, and conceal it. Since column 4 gets drawn last, the x-axis appears turquoise in color (actually, "cyan"), the color for curve four.

However, when you do "plot(fft(A))", you nowhere get an x-axis coordinate of 4!

Result of plot(fft(eye(4)))

[plotfft.gif]

The reason is because fft(A) is a 4x4 matrix with complex values.

>> fft(A)
ans =
   1.0000            1.0000            1.0000            1.0000         
   1.0000                 0- 1.0000i  -1.0000                 0+ 1.0000i
   1.0000           -1.0000            1.0000           -1.0000         
   1.0000                 0+ 1.0000i  -1.0000                 0- 1.0000i
In this case, Matlab plots four curves, but now with the x-coordinate for each curve the real part of the column, and the y-coordinate the imaginary part. The first "curve" consists then of the single point (1,0), plotted four times. This is a corollary to the Matlab "gotcha" described earlier: Matlab invisibly slips into complex numbers whenever appropriate, and for plotting them it does what it thinks is right.


  • Started: Mon Feb 2 14:24:10 EST 2004
  • Last updated: Tue Jan 15 18:26:32 EST 2008