| Contents | Index |
| On this page… |
|---|
You define a surface by the z-coordinates of points above a grid in the x-y plane, using straight lines to connect adjacent points. The mesh and surf plotting functions display surfaces in three dimensions. mesh produces wireframe surfaces that color only the lines connecting the defining points. surf displays both the connecting lines and the faces of the surface in color.
The figure colormap and figure properties determine how the surface is colored.
To display a function of two variables, z = f (x,y),
Generate X and Y matrices consisting of repeated rows and columns, respectively, over the domain of the function.
Use X and Y to evaluate and graph the function.
The meshgrid function transforms the domain specified by a single vector or two vectors x and y into matrices X and Y for use in evaluating functions of two variables. The rows of X are copies of the vector x and the columns of Y are copies of the vector y.
This example evaluates and graphs the two-dimensional sinc function, sin(r)/r, between the x and y directions. R is the distance from the origin, which is at the center of the matrix. Adding eps (a MATLAB command that returns a small floating-point number) avoids the indeterminate 0/0 at the origin:
[X,Y] = meshgrid(-8:.5:8); R = sqrt(X.^2 + Y.^2) + eps; Z = sin(R)./R; mesh(X,Y,Z,'EdgeColor','black')

By default, the current colormap is used to color the mesh. However, this example uses a single-colored mesh by specifying the EdgeColor surface property. See the surface reference page for a list of all surface properties.
You can create a mesh with see-through faces by disabling hidden line removal:
hidden off
See the hidden reference page for more information on this option.
A surface plot is similar to a mesh plot except that the rectangular faces of the surface are colored. The color of each face is determined by the values of Z and the colormap (a colormap is an ordered list of colors). These statements graph the sinc function as a surface plot, specify a colormap, and add a color bar to show the mapping of data to color:
surf(X,Y,Z) colormap hsv colorbar

See the colormap reference page for information on colormaps.
For More Information See Creating 3-D Graphs in the MATLAB 3-D Visualization documentation for more information on surface plots. |
You can make the faces of a surface transparent to a varying degree. Transparency (referred to as the alpha value) can be specified for the whole object or can be based on an alphamap, which behaves similarly to colormaps. For example,
surf(X,Y,Z) colormap hsv alpha(.4)
produces a surface with a face alpha value of 0.4. Alpha values range from 0 (completely transparent) to 1 (not transparent).

For More Information See Manipulating Transparency in the MATLAB 3-D Visualization documentation for details about using this feature. |
Lighting is the technique of illuminating an object with a directional light source. In certain cases, this technique can make subtle differences in surface shape easier to see. Lighting can also be used to add realism to three-dimensional graphs.
This example uses the same surface as the previous examples, but colors it red and removes the mesh lines. A light object is then added to the left of the "camera" (the camera is the location in space from where you are viewing the surface):
surf(X,Y,Z,'FaceColor','red','EdgeColor','none') camlight left; lighting phong

The figure toolbar and the camera toolbar provide ways to explore 3-D graphics interactively. Display the camera toolbar by selecting Camera Toolbar from the figure View menu.
The following picture shows both toolbars with the Rotate 3D tool selected.

These tools enable you to move the camera around the surface object, zoom, add lighting, and perform other viewing operations without issuing commands.
The following picture shows the surface viewed by orbiting the camera toward the bottom using Rotate 3D. You can see the tools's cursor icon on the surface where as you drag to rotate the view. As you drag, the viewing altitude and elevation read out in the lower left corner of the axes, as the picture shows.

For More Information See Lighting as a Visualization Tool and View Control with the Camera Toolbar in the MATLAB 3-D Visualization documentation for details about these techniques. |
![]() | Using Basic Plotting Functions | Plotting Image Data | ![]() |

Includes the most popular MATLAB recorded presentations with Q&A; sessions led by MATLAB experts.
| © 1984-2011- The MathWorks, Inc. - Site Help - Patents - Trademarks - Privacy Policy - Preventing Piracy - RSS |