# Lesson 18: Graphics

MATLAB can produce both planar plots, images, and 3-D mesh surface plots. To preview some of these capabilities and others as well, enter the command demos.

## 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 grapho of the sine function over the interval -4 to 4 with the following commands:
	x = -4:.01:4; y = sin(x); plot(x,y)

Give it a try. 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).

As a second example, we will draw the graph of a exponential function:

	x = -1.5:.01:1.5; y = exp(-x.^2); plot(x,y)

Note that one must precede ^ by a period to ensure that it operates entrywise.

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 specific coordinates

For example, the command
	title('Best Least Squares Fit')

gives the 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 c = [xmin,xmax,ymin,ymax] is a 4-element vector, then axis(c) sets the axis scaling to the prescribed 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. For more informations on axis see help axis.

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

   x=0:.01:2*pi;y1=sin(x);y2=sin(2*x);y3=sin(4*x);plot(x,y1,y2,y3)

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 the hold command. 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.

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);
plot(x,y1,'--',x,y2,':',x,y3,'+')

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-type are
   Linetypes: dashed(--), dotted(:), dashdot(-.), and the default solid(-)
Marktypes: point(.), plus(+), star(*), circle(o), x-mark(x), square(s),
diamond(d), up-triangle(v), down-triangle(^),
left-triangle(<), right-triangle(>), pentagram(p),
hexagram(h)
Colors: yellow(y), magenta(m), cyan(c), red(r), green(g), white(w),
black(k), and the default blue(b)

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.

## Images

Matrices could be viewed as an image, since any digital image is like a matrix in that it has rows and columns of values. The command image(A) will "image" the matrix A. The matrix will be displayed differently depending on the colormap . The colormap can consist of 16 colors or 256 colors; changing how many colors are used will alter the way the matrix is displayed. In order to avoid this alteration due to differences in the number of colors you can use the command imagesc(A) which will automatically scale your matrix to span the entire spectrum of colors possible.

## 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 the 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 gives partitions of the sides of the rectangle. With the function meshgrid (mesh grid) on 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] = meshgrid(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.

The following example will draws the graph of z = exp(-sqr(x)-sqr(y)) over the square [-2,2] \times [-2,2].

  xx = -2:.1:2;
yy = xx;
[x,y] = meshgrid(xx,yy);
z = exp(-x.^2 - y.^2);
mesh(z)

Alternatively, you could replace the first three lines of the preceeding example with:
  [x,y] = meshgrid(-2:.1:2,-2:.1:2);

For more details regarding the command mesh, you can refer to online help or the User's Guide.

Others command related to 3-D graphics are plot3 and surf; where plot3 is used to draw a line in the 3-D space while surf is used to draw a surface in the 3-D space. See the online help for furthur details of these commands.

You can obtain a hardcopy of the graphics screen by using the command print. The print -Pps1 command will send a high-resolution copy of the current graphics screen to the printer ps1, placing the graph on the centre of the page. Also, if you want to print two graphics screen into a file called test.ps, what you can do is:

  plot first graph
print test
plot second graph
print -append test

What the above example do is it will first print the first graph that you have drawn in the file test.ps and the second print statement will append the second graph to the same file. After this, you can print the file using the standard unix command lpr.

Finally, use the close command to close the graphics screen.