Contour and surface plots in Mathematica

Mathematica can make very nice contour and surface plots of three-dimensional functions such as z = z(x, y). The commands that generate these plots are ContourPlot and Plot3D for contour and surface plots, respectively.

These commands have the same basic form, although there are more "options" for Plot3D. To make a plot of some function of x and y in which x ranges from xmin to xmax and y ranges from ymin to ymax, enter

   ContourPlot[ <function>, {x, <xmin>, <xmax>}, {y, <ymin>, <ymax> ]
or
   Plot3D[ <function>, {x, <xmin>, <xmax>}, {y, <ymin>, <ymax> ]
For example, a contour plot of the function Exp[-(x^2 + 3 y^2)] near the origin can be generated as follows:
   ContourPlot[ Exp[-(x^2 + 3 y^2)], {x, -2, 2}, {y, -2, 2} ]
A three-dimensional surface plot of the same function is generated with the command
   Plot3D[ Exp[-(x^2 + 3 y^2)], {x, -2, 2}, {y, -2, 2} ]

If you try these commands, you might find that the surface plot seems "chopped off" near the origin. We can rectify this by including a larger range of z coordinates in the plot, using the PlotRange option that we first saw in two-dimensional plots:

   Plot3D[ Exp[-(x^2 + 3 y^2)], {x, -2, 2}, {y, -2, 2},
           PlotRange -> {0, 2} ]
This command tells Mathematica to include all parts of the function with z values between 0 and 2. We can also use the PlotRange option to specify boundaries on all three coordinates, for example to zoom into a portion of the surface:
   Plot3D[ Exp[-(x^2 + 3 y^2)], {x, -2, 2}, {y, -2, 2} ]
   Show[ %, PlotRange -> {{0, 1}, {0, 1}, {0, 2}} ]

You might notice that the resolution in this "zoomed" plot is fairly poor. Mathematica creates surface plots by scanning over a rectangular grid of points and calculating the height of the surface at each point. When we zoom in, Mathematica continues to use the original grid of points. We can make the grid finer by using the PlotPoints option in Plot3D. (Note that this option does not work in the Show command!) Here is how we might increase the resolution of this zoomed plot:

   Plot3D[ Exp[-(x^2 + 3 y^2)], {x, -2, 2}, {y, -2, 2}, PlotPoints-> 30 ]
   Show[ %, PlotRange -> {{0, 1}, {0, 1}, {0, 2}} ]

Of course, if you decided to zoom in after viewing a surface plot, you can combine the PlotPoints and PlotRange options in the Plot3D command to do everything in one step:

   Plot3D[ Exp[-(x^2 + 3 y^2)], {x, -2, 2}, {y, -2, 2}, PlotPoints-> 30,
           PlotRange -> {{0, 1}, {0, 1}, {0, 2}} ]
Even this is wasteful in a sense; we are generating a 30-by-30 grid of points in which x and y both range from -2 to 2, and then throwing away all of the points except those where x and y are between 0 and 1. Instead, we might try
   Plot3D[ Exp[-(x^2 + 3 y^2)], {x, 0, 1}, {y, 0, 1}, PlotPoints-> 30,
           PlotRange -> {0, 2} ]
which gives us higher resolution in the region of interest.

The PlotPoints option applies to contour plots as well. Compare these two plots:

   ContourPlot[ Exp[-(x^2 + 3 y^2)], {x, -2, 2}, {y, -2, 2} ]

   ContourPlot[ Exp[-(x^2 + 3 y^2)], {x, -2, 2}, {y, -2, 2},
                PlotPoints -> 30 ]
The plot with more points is much smoother, and represents the oval shape of the surface more faithfully. But how can we get rid of the shades of gray so that we can see the contours better? Try
   ContourPlot[ Exp[-(x^2 + 3 y^2)], {x, -2, 2}, {y, -2, 2},
                PlotPoints -> 30, ContourShading -> False ]
Nice, isn't it? The only problem is that we don't know what z values the contour lines correspond to. With the Contours option we can specify exactly where we want the contours. Try entering
   ContourPlot[ Exp[-(x^2 + 3 y^2)], {x, -2, 2}, {y, -2, 2},
                PlotPoints -> 30, ContourShading -> False,
                Contours -> {0.2, 0.4, 0.6, 0.8},
                PlotRange -> {0, 2} ]
to see how you can control the placement of contour lines. Note that we need to include both the Contours and the PlotRange options to make sure that all of the contour values fit inside the range of z values that Mathematica will plot. Remember that when you only give one pair of numbers for PlotRange, Mathematica applies this range to the z axis.

Finally, note that the variables x and y could be called anything we want. These two commands produce exactly the same results:

   ContourPlot[ Exp[-(x^2 + 3 y^2)], {x, -2, 2}, {y, -2, 2} ]

   ContourPlot[ Exp[-(u^2 + 3 v^2)], {u, -2, 2}, {v, -2, 2} ]


Other parts of the Mathematica tutorial: