Three-Dimensional Surface Plots

Use a three-dimensional surface when height helps explain a function of two inputs. Align coordinate and height grids, control the projected viewpoint, compare surfaces fairly, and prefer contours when levels or paths need precise reading.

A three-dimensional surface plot uses two coordinate axes for a function's inputs and height for its output. For a loss with parameters weight and bias, one surface point has coordinates

(weight, bias, loss)

Height can make valleys, peaks, flat regions, and overall curvature intuitive. The surface is still projected onto a flat screen, however. Viewing angle, perspective, hidden regions, and axis scaling can make exact comparisons harder than they are in a contour plot.

Build one height for each coordinate pair

Use the same three grids introduced for contours:

All three arrays have shape (81, 81). At one grid position:

weight_grid[row, column] -> first horizontal coordinate
bias_grid[row, column]   -> second horizontal coordinate
loss_grid[row, column]   -> height

The shape agreement is not merely a Matplotlib requirement. It preserves the meaning that one weight and one bias produced one loss.

Draw a surface with height and color

Matplotlib creates a three-dimensional plotting area by passing projection="3d":

plot_surface then receives the three matching grids. Run the complete example and edit the coefficient of the bias term to change the bowl's shape.

Inspect a two-parameter loss surface

Height and color both encode loss. Change the bias coefficient from 2 to 0.5 or 4 and inspect how the surface changes in that direction.

Ready to run.

The surface is a bowl with its lowest point at (2, -1, 0). Color repeats the height information and helps distinguish nearby regions. It does not add a fourth variable in this example.

Understand the flat projection

The figure appears three-dimensional because Matplotlib projects 3D coordinates onto the two-dimensional page. Some points lie behind others, and the visible size of a region depends on the viewpoint.

Set a reproducible view with:

axes.view_init(elev=30, azim=45)

elev is the elevation angle above the horizontal parameter plane. azim is the direction around the vertical axis. Changing either value changes the view, not the parameter grids or losses.

A low elevation can emphasize height but hide the far side of a valley. A high elevation approaches a top-down view but reduces the visible sense of height. Inspect more than one view while exploring, then choose and record the view that communicates the intended structure without hiding important regions.

Compare a surface with its contours

The same sampled function can be shown in both forms. The next example uses one set of coordinate and loss grids, so the difference comes from representation rather than data.

Compare height with equal-loss levels

The left panel emphasizes overall height. The right panel preserves the parameter plane and labels equal-loss curves.

Ready to run.

The surface makes the bowl-like geometry immediate. The contour plot makes equal values, parameter-plane distances, and possible optimization paths easier to compare. Use both when each answers a distinct question; otherwise prefer the simpler view.

Choose between a filled surface and a wireframe

plot_surface draws filled faces. A wireframe draws selected grid lines:

rstride and cstride select the step between displayed row and column grid lines. A wireframe can reveal sampling structure and leave more of the far side visible, but dense lines can become cluttered. A filled surface usually gives a clearer overall form. Choose from the inspection question, not from which style looks more elaborate.

Separate grid resolution from visual smoothness

A surface joins sampled grid values into faces. More coordinate samples can make those faces appear smoother:

21 × 21 grid -> 441 sampled points
81 × 81 grid -> 6,561 sampled points

The smoother rendering is not evidence that the underlying function or data is more accurate. Resolution should be fine enough to reveal the relevant structure without wasting computation. Sharp changes between samples can still be missed.

Surface plotting functions may also reduce the number of displayed samples for rendering efficiency. Keep the numerical grid and its resolution available when exact sampling matters.

Keep comparisons numerically fair

When comparing two surfaces, use the same:

  • parameter ranges;
  • coordinate sampling;
  • horizontal and vertical axis limits;
  • viewing angle;
  • height limits;
  • colormap and color limits.

Different height limits or viewpoints can make similar surfaces appear different. The apparent steepness also depends on the visual length assigned to each axis. Read slopes from numerical coordinates and loss changes rather than screen angle alone.

State what the surface represents

This example is genuinely a function of two parameters. A large model has many more. Its displayed surface may be:

  • a toy loss designed for explanation;
  • two selected parameters with all others fixed;
  • or two selected directions through the full parameter space.

Different slices through the same high-dimensional loss can have different shapes. A smooth bowl in one slice does not prove that every direction is smooth, that the full problem has one minimum, or that optimization is easy.

Exercise: Identify the height coordinate

A surface uses weight and bias on its two horizontal axes. What quantity belongs on the vertical axis for the function loss(weight, bias)?

Answer it first, then check.

HintName the function output

A surface point has coordinates (weight, bias, loss).

SolutionHeight represents loss

The vertical coordinate is the loss returned for each weight–bias pair.

Exercise: Check the three grid shapes

weight_grid and bias_grid both have shape (60, 80). What shape must loss_grid have for each position to provide one complete surface point?

Answer it first, then check.

HintPreserve one height per pair

Every coordinate-grid position needs a loss at the same position.

SolutionThe loss grid also has shape (60, 80)

Matching shapes preserve one weight, one bias, and one loss at each grid index.

Exercise: Change only the viewpoint

What happens when this call changes from axes.view_init(elev=20, azim=30) to axes.view_init(elev=50, azim=120)?

Choose the effect

Select one choice, then check.

HintSeparate data from camera

view_init receives angles, not weight, bias, or loss arrays.

SolutionOnly the displayed projection changes

Elevation and azimuth alter the viewpoint. They do not change any sampled coordinate or loss.

Exercise: Choose a view for an optimization path

You need to compare a parameter path with exact equal-loss levels while preserving distances in the weight–bias plane. Which plot is usually clearer?

Answer it first, then check.

HintPreserve the parameter plane

The path and equal-loss curves need to share two unobstructed coordinate axes.

SolutionUse a contour plot

Contours show the parameter path against labeled equal-loss curves without perspective or occlusion.

Exercise: Describe a high-dimensional surface honestly

A figure shows loss while moving in two selected directions through a model with one million parameters. What does the figure represent?

Choose the description

Select one choice, then check.

HintCount what remains visible

The figure has two parameter axes even though the model has many more.

SolutionIt is a two-dimensional slice

All other parameter directions are absent or fixed. The visible shape cannot describe the full loss surface.

Use height only when it improves the explanation

A surface plot is useful when visible height clarifies the overall geometry of a function of two inputs. Verify the three grid shapes, record the viewpoint and scales, compare important values numerically, and use contours when levels, paths, or parameter-plane distances need more precise reading.