1. Home
  2. Data & files

Plotting · 3D

Mesh Plot with Contours: Building a meshc-Style Figure

Eigenfield editorial team ·

A mesh plot with contours combines two views of one function of two variables: a wireframe surface in three dimensions, and a set of contour lines drawn on the plane beneath it. The wireframe shows the overall shape, while the projected lines let the reader pick off heights that perspective makes hard to judge. Many matrix languages package this as a single command often named meshc. This page explains the construction and gives guidance on grid size, hidden lines and contour levels. It belongs to the plotting section of Data and files.

Building the grid

A mesh needs values of z on a rectangular grid. Choose the ranges for x and y, decide how many points each direction gets, and evaluate the function at every pair. With 21 points from −2 to 2 the spacing is 4 / 20 = 0.2. A 21 by 21 grid has 441 vertices, and since each cell between four neighbors becomes one quadrilateral, there are 20 × 20 = 400 cells.

import numpy as np
x = np.linspace(-2, 2, 21)
y = np.linspace(-2, 2, 21)
X, Y = np.meshgrid(x, y)
Z = np.sin(X) * np.cos(Y)

The meshgrid call repeats the x values down the rows and the y values across the columns, so that X[i, j] and Y[i, j] hold the coordinates of vertex (i, j). A frequent mistake is to pass Z with its dimensions swapped relative to x and y; if the plot looks like the function reflected across the diagonal, transpose Z.

Choosing the density

A coarse grid misses features: a peak narrower than two grid steps may vanish or appear as a single spike. A very fine grid produces a wireframe so dense that it reads as a solid gray surface and slows drawing. A practical range for a mesh is 20 to 60 points per direction. If the function has a narrow feature, refine the grid there instead of everywhere, or plot the data as a surface with shading instead.

The contour layer

The contour lines come from the same matrix Z, so they need no extra computation by the user. They are drawn at a fixed height, usually the minimum of the z-axis, which is why they look like a map placed under the surface. Contour levels follow the rules described under contour levels: even spacing by default, round values when readable labels matter. For sin(x)cos(y) the values lie between −1 and 1, and levels at −0.8, −0.4, 0, 0.4 and 0.8 use the full range with round numbers.

Because the contours are projections, each line connects points of equal z, and when the wireframe rises steeply above a dense cluster of lines, the slope is steep. Looking for this match between the two layers is the quickest way to read the figure.

Hidden lines and viewing angle

A wireframe of an opaque surface hides the lines behind it, which is why a mesh looks solid from some angles and transparent from others. If the hidden-line option is off, every edge is drawn and the plot becomes a tangle; turning it on gives the cleaner view. Pick an azimuth and elevation that show the peaks without letting them block valleys. An elevation of 25 to 40 degrees and an azimuth of about −35 degrees are common starting points, and rotating the view for a minute is better than trusting a default.

Limits of the combined view

The contour layer adds clutter when the surface itself carries many features. In that case plot the two views side by side instead: the wireframe on the left, a filled contour map on the right. For a shaded version of the same idea, see surface plots with contours. Where the axes are placed affects how the base plane reads, which plot axis intercepts explains.

Back to top