1. Home
  2. Data & files

Plotting · 3D

Surface Plot with Contours: Shaded Facets and Base Lines

Eigenfield editorial team ·

A surface plot with contours fills each cell of a gridded function with color, then adds contour lines on the plane below. It answers the same question as a mesh plot, namely what a function of two variables looks like, but with solid facets that hide what lies behind them and with color carrying a value. The result is easier to read at a glance and more tolerant of dense grids. This page covers how the facets are shaded, how color maps to values, and how to choose between this view and the wireframe version described under mesh plots with contours. The material is part of Data and files.

How the facets are colored

Each grid cell is a small quadrilateral. In flat shading the whole cell gets one color, taken from one of its corners or from the average of the four. In interpolated shading the color varies smoothly across the cell, blending the corner values. Interpolated shading looks smoother and hides the grid, but it can mislead: a smooth gradient suggests a continuous function even when the grid is coarse. Flat shading is more honest about resolution.

By default the color represents the height z, which duplicates what the vertical axis already says. That redundancy is useful, because a color bar lets the reader read heights from any angle. When the color should carry a second variable, say temperature on the surface of a height field, supply a separate matrix of the same size for the color and keep its colorbar labelled.

Choosing a color scale

Scales that move through several hues, such as the rainbow, create false boundaries where the hue changes and are hard for readers with color-vision differences. A scale that changes mainly in lightness, with a gentle shift of hue, shows magnitude more faithfully. For data that are signed around a meaningful center, such as the saddle z = x² − y² that runs from −4 to 4 on a grid from −2 to 2, use a diverging scale with a neutral color at zero and make the limits symmetric: −4 to 4, not −4 to 3.9.

import numpy as np, matplotlib.pyplot as plt
x = y = np.linspace(-2, 2, 41)
X, Y = np.meshgrid(x, y)
Z = X**2 - Y**2
ax = plt.figure().add_subplot(projection="3d")
ax.plot_surface(X, Y, Z, cmap="coolwarm", vmin=-4, vmax=4)
ax.contour(X, Y, Z, zdir="z", offset=Z.min(), levels=9)

Check the extremes by hand: at x = 2, y = 0 the height is 4, and at x = 0, y = 2 it is −4. The corners x = ±2, y = ±2 give 0.

The contour layer

Lines on the base plane repeat the structure of the surface as a map. For the saddle above, the zero contour is the pair of lines y = x and y = −x, and the lines at other levels are hyperbolas, so the base map shows at once where the surface switches from rising to falling. Level selection follows the rules in contour levels, and nine levels from −4 to 4 in steps of one give a readable map.

Lighting and edges

Facets drawn with black edges at every cell become a wireframe in disguise. Turn the edges off or make them very light when the grid is finer than about 30 points a side. A light source from the upper left adds shading that exposes curvature, especially where color alone is flat. Use one light; several create competing highlights that hide the surface.

Choosing between the two views

Pick the surface plot for presentation, for dense grids, and when a color scale is wanted. Pick the wireframe when the grid itself matters, when the surface is partly see-through by design, or when the figure will be reproduced in black and white. In both, where the axes cross shapes the reading of the base plane, explained under plot axis intercepts.

Back to top