What Is Plot3D?

Plot3D draws the graph of a function of two variables, z = f(x, y), as a surface over a rectangle of the plane. You give it the function and a range for each variable:

Plot3D[f[x, y], {x, -4, 4}, {y, -4, 4}]

Mathematica samples the function on a grid, adds more points where the surface bends, and returns a 3D graphic. In a notebook you can drag it with the mouse to turn it around.

The worked example. Every plot on this page draws the same function:

f[x_, y_] := Sin[x] Cos[y] Exp[-(x^2 + y^2)/10]

The sine and cosine make a checkerboard of hills and valleys, and the exponential factor flattens them as you move away from the origin. The highest point is about 0.81, at x ≈ 1.31 and y = 0; the lowest is about −0.81, its mirror image at x ≈ −1.31.
Mid-century illustration of a figure at the top of a short flight of stairs under a low arched ceiling

Remember

Four things to keep in mind about Plot3D.

  • Plot3D takes a function of two variables and one range for each: {x, xmin, xmax} and {y, ymin, ymax}.

  • Define the function once with := and plot it by name, so every plot draws the same surface.

  • Options go after the ranges, written Name -> value. They change how the surface looks, never the function.

  • Mesh, colors and lighting are all options: the default orange surface is only a starting point.

Reading the Worked Example

Each panel adds one or two options to the plot before it.

1. The default. Plot3D[f[x, y], {x, -4, 4}, {y, -4, 4}] draws an orange surface with a grid of mesh lines. The sampling, the colors and the lighting are all automatic.

2. Color by height. ColorFunction colors each point by its height; in the function you give it, #3 stands for z. Here the colors run from navy in the valleys to coral on the peaks, with Mesh -> None to drop the grid and PlotPoints -> 50 for a smoother surface.

3. Contour lines. MeshFunctions -> {#3 &} with Mesh -> 12 replaces the grid with 12 lines of constant height, the way a topographic map marks elevation.

4. A contour map underneath. A second Plot3D draws a flat plane at z = −1.5, colored and meshed by the same function, and Show[surface, floor] puts the two together. Each ring on the floor lies directly under the hill or valley it describes. The FAQ below has the code.
Four Plot3D panels of the same function: the default orange surface, the surface colored by height, contour lines on the surface, and a contour map laid underneath

Three Things to Do with a Surface

  • Put a Contour Map Under It

    Draw the surface, draw a flat plane below it colored by the same function, and combine the two with Show. The fourth panel above is made this way; the FAQ has the code.

  • Mark Points on It

    Plot3D draws surfaces, not points. Draw the points with Graphics3D and Point, giving each one the height f(x, y) so it sits on the surface, then combine the plot and the points with Show.

  • Plot Over a Region

    RegionFunction -> Function[{x, y, z}, x^2 + y^2 < 9] keeps only the part of the surface above a disk of radius 3. Any condition on x, y and z works.

FAQ

  • What does Plot3D do in Mathematica?

    It draws the surface z = f(x, y) for a function of two variables over a rectangle of x and y values. The basic form is Plot3D[f[x, y], {x, xmin, xmax}, {y, ymin, ymax}].

  • How do I make a Plot3D surface smoother?

    Raise PlotPoints, the number of sample points in each direction (try 50 to 100), and MaxRecursion, how many times Mathematica may subdivide where the surface bends. Sharp peaks and narrow valleys gain the most.

  • How do I label the axes of a 3D plot?

    Add AxesLabel -> {"x", "y", "z"} for the axes and PlotLabel -> "My surface" for a title.

  • How do I put a contour plot under a Plot3D surface?

    Draw the surface and a flat plane below it, color and mesh the plane by the same function, and combine them with Show:

    surface = Plot3D[f[x, y], {x, -4, 4}, {y, -4, 4}, MeshFunctions -> {#3 &}, Mesh -> 12];
    floor = Plot3D[-1.5, {x, -4, 4}, {y, -4, 4}, MeshFunctions -> {f[#1, #2] &}, Mesh -> 12, ColorFunction -> Function[{x, y, z}, ColorData["SunsetColors"][(f[x, y] + 0.82)/1.64]], ColorFunctionScaling -> False];
    Show[surface, floor, PlotRange -> {-1.5, 1}]

    The 0.82 and 1.64 rescale the function, which runs from about −0.81 to 0.81, onto the 0-to-1 range a color scheme expects.

  • What is the difference between Plot3D and ParametricPlot3D?

    Plot3D draws one height z above each point (x, y), so its surface never folds over itself. ParametricPlot3D gives x, y and z each as a function of two parameters, so it can draw spheres, tori and surfaces that curl back on themselves.

Learn to Plot, Solve and Compute in Mathematica

Our Mathematica Essentials course starts with your first notebook and covers functions, lists, 2D and 3D graphics, solving equations, derivatives and integrals, and working with data, in short video lessons with quizzes.