3D graphs and other graphics capabilities

3D plotting

When you make a 3-dimensional plot, you usually have a z variable that is a function of both x and y. When you want x and y to vary over some range, you need a matrix (rather than a vector) for x and y to get a complete domain that covers all the different combinations of those x and y values over some range. A function called meshgrid will set up x and y matrixes like this for you. The x matrix varies the x down rows and keeps it constant in columns, and the y matrix varies the y in columns and keeps it constant across rows, so you get all combinations of x and y if you use the two matrices.

To get a rectangular domain that goes from x=1 to x=10 in steps of .5, and from y=1 to y=10 in steps of .5 using meshgrid, you can use

[x, y] = meshgrid([1:.5:10],[1:.5:10]);
Then, you can do a 3d plot by caculating some z values on the x and y domains, and doing a surface, mesh, or contour plot.

z = x.^2 - y.^2
surf(x,y,z)
mesh(x,y,z)
contour(x,y,z)
You could also use surfc or meshc to get surface or mesh plots with a contour plot drawn on the x-y plane. Surface plots and mesh plots color the plots according to the z value, by default. You can use some other equally-sized matrix to get the coloring instead by adding that matrix as an extra argument, like:

c = x.^2 + y.^2;
mesh(x,y,z,c)

Colormaps and Visibility

The default colormap that Matlab uses is called 'hsv'. This colormap cycles all the way around from red to red, so both lows and highs will look red. This is often not very useful in engineering applications. You can switch to a colormap that goes from blue to red with

colormap('jet')
If you type "help hsv", the see also list includes the other predefined colormaps.

You can get a colorbar that indicates what colors correspond to what values with

colorbar
Sometimes, a 3d surface plot may cover up portions of its plot. You can take advantage of the fact that Matlab doesn't plot NaN's and Inf's (remember the debugging problem?) to make "cutouts" in a plot. An example of this is in an m-file in the homework directory (/mit/2.670/Computers/Matlab/Examples), so you should be able to run it by typing "NaNdemo" at the matlab prompt , and see how the cutout was made by typing "type NaNdemo".

Adding Text

The command
text(x,y,'Some Text Here')
lets you place arbitrary text at some position on your graph. You give it x and y values (using the axes on your current plot), and you can put text at that location.
gtext('Some Text Here')
lets you place text using the mouse button.

Another useful mouse command is "ginput". If you type

[xclick yclick] = ginput
you can click any number of points in your graph, and hit return in the graph window when you are done. It will save the x and y coordinates of the points you have clicked in the vectors xclick and yclick. This is especially useful if you have something like a plot of a decaying sine wave, and you want to know about the exponential decay of the peaks instead of worrying about the whole sinusoidal part. You can just click on each peak with the mouse in ginput, and then analyze the exponential decay of those points with a curvefit or something similar.

(This was not covered in class, but it is also interesting and useful...) Another useful set of commands, in the "graphics" toolbox, are stext, sxlabel, sylabel, stitle, etc. Read the help on stext for a basic idea of using them. You can do things like combine greek fonts, italicized fonts, different font sizes, etc into your graph, fairly easily, using latex-like formatting commands.)