This is documentation for Mathematica 3, which was
based on an earlier version of the Wolfram Language.
View current documentation (Version 11.2)
 Documentation / Mathematica / The Mathematica Book / Practical Introduction / Graphics and Sound  /

1.9.7 Three-Dimensional Surface Plots

Basic 3D plotting function.

  • This makes a three-dimensional plot of the function

  • In[1]:= Plot3D[Sin[x y], {x, 0, 3}, {y, 0, 3}]

    There are many options for three-dimensional plots in Mathematica. Some will be discussed in this section; others will be described in Section 2.9.
    The first set of options for three-dimensional plots is largely analogous to those provided in the two-dimensional case.

    Some options for Plot3D. The first set can also be used in Show.

  • This redraws the plot on the previous line, with options changed. With this setting for PlotRange, only the part of the surface in the range

    is shown.
  • In[2]:= Show[%, PlotRange -> {-0.5, 0.5}]

  • When you make the original plot, you can choose to sample more points. You will need to do this to get good pictures of functions that wiggle a lot.
  • In[3]:= Plot3D[10 Sin[x + Sin[y]], {x, -10, 10}, {y, -10, 10},
    PlotPoints -> 40]

  • Here is the same plot, with labels for the axes, and grids added to each face.
  • In[4]:= Show[%, AxesLabel -> {"Time", "Depth", "Value"},
    FaceGrids -> All]

    Probably the single most important issue in plotting a three-dimensional surface is specifying where you want to look at the surface from. The ViewPoint option for Plot3D and Show allows you to specify the point

    x,y,z in space from which you view a surface. The details of how the coordinates for this point are defined will be discussed in Section 2.9.10. In many versions of Mathematica, there are ways to choose three-dimensional view points interactively, then get the coordinates to give as settings for the ViewPoint


  • Here is a surface, viewed from the default view point {1.3,-2.4,2}. This view point is chosen to be "generic", so that visually confusing coincidental alignments between different parts of your object are unlikely.
  • In[5]:= Plot3D[Sin[x y], {x, 0, 3}, {y, 0, 3}]

  • This redraws the picture, with the view point directly in front. Notice the perspective effect that makes the back of the box look much smaller than the front.
  • In[6]:= Show[%, ViewPoint -> {0, -2, 0}]

    Typical choices for the ViewPoint option.

    The human visual system is not particularly good at understanding complicated mathematical surfaces. As a result, you need to generate pictures that contain as many clues as possible about the form of the surface.
    View points slightly above the surface usually work best. It is generally a good idea to keep the view point close enough to the surface that there is some perspective effect. Having a box explicitly drawn around the surface is helpful in recognizing the orientation of the surface.

  • Here is a plot with the default settings for surface rendering options.
  • In[7]:= g = Plot3D[Exp[-(x^2+y^2)], {x, -2, 2}, {y, -2, 2}]

  • This shows the surface without the mesh drawn. It is usually much harder to see the form of the surface if the mesh is not there.
  • In[8]:= Show[g, Mesh -> False]

  • This shows the surface with no shading. Some display devices may not be able to show shading.
  • In[9]:= Show[g, Shading -> False]

    The inclusion of shading and a mesh are usually great assets in understanding the form of a surface. On some vector graphics output devices, however, you may not be able to get shading. You should also realize that when shading is included, it may take a long time to render the surface on your output device.
    To add an extra element of realism to three-dimensional graphics, Mathematica by default colors three-dimensional surfaces using a simulated lighting model. In the default case, Mathematica assumes that there are three light sources shining on the object from the upper right of the picture. Section 2.9.12 describes how you can set up other light sources, and how you can specify the reflection properties of an object.
    While in most cases, particularly with color output devices, simulated lighting is an asset, it can sometimes be confusing. If you set the option Lighting->False, then Mathematica will not use simulated lighting, but will instead shade all surfaces with gray levels determined by their height.

  • Plot3D usually colors surfaces using a simulated lighting model.
  • In[10]:= Plot3D[Sin[x y], {x, 0, 3}, {y, 0, 3}]

  • Lighting->False switches off the simulated lighting, and instead shades surfaces with gray levels determined by height.
  • In[11]:= Show[%, Lighting -> False]

    With Lighting->False, Mathematica shades surfaces according to height. You can also tell Mathematica explicitly how to shade each element of a surface. This allows you effectively to use shading to display an extra coordinate at each point on your surface.

    Specifying shading functions for surfaces.

  • This shows a surface whose height is determined by the function Sin[xy], but whose shading is determined by GrayLevel[x/3].
  • In[12]:= Plot3D[{Sin[x y], GrayLevel[x/3]},
    {x, 0, 3}, {y, 0, 3}]