Next: Reference Up: No Title Previous: Hardcopy


MATLAB can produce both planar plots and 3-D mesh surface plots. To preview some of these capabilities in version 3.5, enter the command plotdemo.

Planar plots. The plot command creates linear x-y plots; if x and y are vectors of the same length, the command plot(x,y) opens a graphics window and draws an x-y plot of the elements of x versus the elements of y. You can, for example, draw the graph of the sine function over the interval -4 to 4 with the following commands:

  x = -4:.01:4;  y = sin(x);  plot(x,y)
Try it. The vector x is a partition of the domain with meshsize 0.01 while y is a vector giving the values of sine at the nodes of this partition (recall that sin operates entrywise).

When in the graphics screen, pressing any key will return you to the command screen while the command shg (show graph) will then return you to the current graphics screen. If your machine supports multiple windows with a separate graphics window, you will want to keep the graphics window exposed-but moved to the side-and the command window active.

As a second example, you can draw the graph of over the interval -1.5 to 1.5 as follows:

x = -1.5:.01:1.5;  y = exp(-x.^2);  plot(x,y)
Note that one must precede the power-to sign by a period to ensure that it operates entrywise (see section 3).

Plots of parametrically defined curves can also be made. Try, for example,

t=0:.001:2*pi; x=cos(3*t); y=sin(2*t); plot(x,y)

The command grid will place grid lines on the current graph.

The graphs can be given titles, axes labeled, and text placed within the graph with the following commands which take a string as an argument.

title         graph title  
xlabel        x-axis label  
ylabel        y-axis label  
gtext         interactively-positioned text  
text          position text at specified coordinates
For example, the command
title('Best Least Squares Fit')
gives a graph a title. The command gtext('The Spot') allows a mouse or the arrow keys to position a crosshair on the graph, at which the text will be placed when any key is pressed.

By default, the axes are auto-scaled. This can be overridden by the command axis. If is a 4-element vector, then axis(c) sets the axis scaling to the precribed limits. By itself, axis freezes the current scaling for subsequent graphs; entering axis again returns to auto-scaling. The command axis('square') ensures that the same scale is used on both axes. In version 4.0, axis has been significantly changed; see help axis.

Two ways to make multiple plots on a single graph are illustrated by

and by forming a matrix Y containing the functional values as columns
x=0:.01:2*pi; Y=[sin(x)', sin(2*x)', sin(4*x)']; plot(x,Y)
Another way is with hold. The command hold freezes the current graphics screen so that subsequent plots are superimposed on it. Entering hold again releases the ``hold.'' The commands hold on and hold off are also available in version 4.0.

One can override the default linetypes and pointtypes. For example,

        x=0:.01:2*pi; y1=sin(x); y2=sin(2*x); y3=sin(4*x);   
renders a dashed line and dotted line for the first two graphs while for the third the symbol is placed at each node. The line- and mark-types are

Linetypes: solid (-), dashed (-). dotted (:), dashdot (-.)

Marktypes: point (.), plus (), star (*), circle (o), x-mark (x)

See help plot for line and mark colors.

The command subplot can be used to partition the screen so that up to four plots can be viewed simultaneously. See help subplot.

Graphics hardcopy Note that most of this section is no longer relevant

A hardcopy of the graphics screen can be most easily obtained with the MATLAB command print. It will send a high-resolution copy of the current graphics screen to the printer, placing the graph on the top half of the page.

In version 4.0 the meta and gpp commands described below have been absorbed into the print command. See help print.

Producing unified hard copy of several plots requires more effort. The Matlab command meta filename stores the current graphics screen in a file named filename.met (a ``metafile'') in the current directory. Subsequent meta (no filename) commands append a new current graphics screen to the previously named metafile. This metafile-which may now contain several plots-may be processed later with the graphics post-processor (GPP) program to produce high-resolution hardcopy, two plots per page.

The program GPP (graphics post-processor) is a system command, not a MATLAB command. However, in practice it is usually involked from within MATLAB using the "!" feature (see section 14). It acts on a device-independent metafile to produce an output file appropriate for many different hardcopy devices.

The selection of the specific hardcopy device is made with the option key "/d". For example, the system commands

gpp  filename  /dps    
gpp  filename  /djet
will produce files and filename.jet suitable for printing on, respectively, PostScript and HP LaserJet printers. They can be printed using the usual printing command for the local system-for example, lpr on a Unix system. Entering the system command gpp with no arguments gives a list of all supported hardcopy devices. On a PC, most of this can be automated by appropriately editing the file gpp.bat distributed with MATLAB.

Note that in newer versions of Matlab the above text is no longer relevant

3-D mesh plots. Three dimensional mesh surface plots are drawn with the function mesh. The command mesh(z) creates a three-dimensional perspective plot of the elements of the matrix z. The mesh surface is defined by the z-coordinates of points above a rectangular grid in the x-y plane. Try mesh(eye(10)).

To draw the graph of a function z=f(x,y) over a rectangle, one first defines vectors xx and yy which give partitions of the sides of the rectangle. With the function meshdom (mesh domain; called meshgrid in version 4.0) one then creates a matrix x, each row of which equals xx and whose column length is the length of yy, and similarly a matrix y, each column of which equals yy, as follows:

[x,y] = meshdom(xx,yy);
One then computes a matrix z, obtained by evaluating f entrywise over the matrices x and y, to which mesh can be applied.

You can, for example, draw the graph of over the square [-2,2] x [-2,2] as follows (try it):

        xx = -2:.1:2;    
        yy = xx;    
        [x,y] = meshdom(xx,yy);     
        z = exp(-x.^2 - y.$^2);    
One could, of course, replace the first three lines of the preceding with

 [x,y] = meshdom(-2:.1:2, -2:.1:2);

You are referred to the User's Guide for further details regarding mesh.

In version 4.0, the 3-D graphics capabilities of MATLAB have been considerably expanded. Consult the on-line help for plot3, mesh, and surf.

Next: Reference Up: No Title Previous: Hardcopy

Wed Mar 13 19:15:55 MET 1996