I.6Three - Dimensional Graphics

I.6.1Introduction

Three-dimensional (3D) data can be displayed in many different styles. Line, surface, and wire mesh are examples of displaying 3D plots.

I.6.2Line Plots

The plot3 command in MATLAB is used to display data using connected lines. A simplified format for this command is:

The 3-D Line Plot Command

plot3(x, y, z, s)

The argument of plot3 is a four-tuple(having four vectors), where x, y, and z are three vectors of the same size. For each x, y, and z, there is a unique location in 3D space. The plot3 command uses lines to connect these points with the style defined by s. If s is eliminated, then MATLAB uses the default setting.

Example I.6.2.1:

» x = -10:0.01:10;

» y = sin(x);

» z = sin(x + pi / 3);

» plot3(y, z, x)

»title(‘Coil’), xlabel(‘sin(x)’), ylabel(‘(sin(x + pi / 3)’), zlabel(‘x’)

3-D Line Plot

{Do not alter this page}

You can plot more than one set of 3D data on the same set of axes. This can be accomplished by adding four-tuples to the argument of the plot3 command. For example:

plot3(x1,y1,z1,s1,x2,y2,z2,s2)

displays two sets of three dimensional data, x1,y1,z1 and x2,y2,z2 using styles s1 an s2, respectively. The line types, line colors, and marker symbols are specified by s in the argument of plot3. A set of line styles, colors, and markers is given in the following table:

Table I.6.2.1

SymbolColorSymbolMarkerSymbolLine Style

yyellow . point - solid

m magenta o circle : dotted

c cyan x x-mark -. dashdot

r red + plus -- dashed

g green * star

b blue s square

w white d diamond

k black v triangle (down)

^ triangle (up)

< triangle (left)

> triangle (right)

p pentagram

h hexagram

Example I.6.2.2:

» x = -10:0.1:10;

» y1 = sin(x);

» z1 = sin(x + pi / 3);

» y2 = sin(x + pi / 4);

» z2 = sin(x + pi / 6);

» plot3(y1, z1, x, '-', y1, y2, x, '+ - .')

» title('Tangled Coils')

» xlabel('y1')

» ylabel(‘z1, y2')

»

3-D Plot

{Do not alter this page}

I.6.3Mesh Plots

Mesh plots are useful for plotting functions of two variables such as z =f (x,y).The mesh surface is defined by the z-coordinates of points above a rectangular grid in the x-y plane. The mesh command forms a plot by joining adjacent points with straight lines. There are four steps involved in generating mesh plots:

Step 1. Define a desired range for two variables, say x and y.

Step 2. Use the meshgrid command to generate a mesh related to x and y:

[x, y] = meshgrid(x, y).

Step 3. Evaluate the function f(x, y), say z = sqrt(x * x + y * y).

Step 4. Use the mesh command to plot the mesh corresponding to z: mesh(x, y, z).

Example I.6.3.1:

Plot a mesh graph for

over the range -10 x  10 and -10  y 10.

» x = -10:1:10;

» y = x;

» [x, y] = meshgrid(x, y);

» z = sqrt(x.^2 + y.^2);

» mesh(x, y, z)

»

3-D Plot

{Do not alter this page}

I.6.4Surface Plots

A surface plot is the same as a mesh plot except that the patches (the space between the lines) are filled with a specified (or default) color. All of the steps involved in plotting a surface graph are the same as the ones for mesh plots except for step 4, where mesh is replaced by surf.

Step 1. Define a desired range for two variables, say x and y.

Step 2. Use the meshgrid command to generate a mesh related to x and y:

[x, y] = meshgrid(x, y).

Step 3. Evaluate the function f(x, y), say z = sqrt(x * x + y * y).

Step 4. Use the surf command to plot the surface corresponding to z: surf(x, y, z).

Example I.6.4.1:

Plot a surface graph for

over the range -10 x  10 and -10  y 10.

» x = -10:1:10;

» y = x;

» [x, y] = meshgrid(x, y);

» z = sqrt(x.^2 + y.^2);

» surf(x, y, z)

»

3-D Plot

{Do not alter this page}

I.6.5 Contour Plots

The contour command draws a contour plot of a function of two variables such as z=f(x,y). The number of contour lines and their values are chosen automatically by the contour command.

The Contour Command

contour(x, y, z, n)

where z is a function of x and y. The parameter n defines the number of contours to be plotted by the contour command. However, if n is deleted, then the contour command automatically chooses the number of contours. The steps involved in drawing a contour plot of a function of two variables are the same steps used for mesh plots except in step 4, where the mesh command is replaced by the contour command.

Step 1. Define a desired range for two variables, say x and y.

Step 2. Use the meshgrid command to generate a mesh related to x and y:

[x, y] = meshgrid(x, y).

Step 3. Evaluate the function f(x, y), say z = sqrt(x * x + y * y).

Step 4. Use the contour command to plot the contours corresponding to z:

contour(x, y, z).

Example I.6.5.1:

Plot a contour graph for

over the range -10 x  10 and -10  y 10.

» x = -10:1:10;

» y = x;

» [x, y] = meshgrid(x, y);

» z = sqrt(x.^2 + y.^2);

» contour(x, y, z)

»

2-D Contour Plot

{Do not alter this page}

If you want to apply a larger number of contours, simply specify the desired number in the last argument of the contour command. For example, to plot 20 contours, type

contour(x, y, z, 20) instead of contour(x, y, z).

I.6.6Three-Dimensional Contour Plots

To produce a 3-D contour of a 3D function, you can use the contour3 command. The argument of the contour3 command is the same as the argument of the contour command. The steps involved in generating a 3-D contour plot are the same as those of the mesh command except that in step 4, the mesh command is replaced by the contour3 command.

Step 1. Define a desired range for two variables, say x and y.

Step 2. Use the meshgrid command to generate a mesh related to x and y:

[x, y] = meshgrid(x, y).

Step 3. Evaluate the function f(x, y), say z = sqrt(x * x + y * y).

Step 4. Use the contour3 command to plot the 3-D contours corresponding to z:

contour3(x, y, z).

Example I.6.6.1:

Plot a 3-D contour plot for

over the range -10 x  10 and -10  y 10.

» x = -10:1:10;

» y = x;

» [x, y] = meshgrid(x, y);

» z = sqrt(x.^2 + y.^2);

» contour3(x, y, z)

»

2-D Contour Plot

{Do not alter this page}