Contour Plot
NIST/SEMATECH Section 1.3.3.10 Contour Plot
What It Is
A contour plot is a graphical technique for representing a three-dimensional surface by plotting constant slices, called contours, on a two-dimensional format. Given a value for , lines are drawn connecting the coordinates where that value occurs. The contour plot is an alternative to a 3-D surface plot.
The contour plot is formed with the horizontal axis as one independent variable, the vertical axis as the other independent variable, and the contour lines as iso-response values. The techniques for determining correct iso-response values are complex and almost always computer-generated. If the data (or function) do not form a regular grid, a 2-D interpolation is typically needed. Color fills between contours are optional but common. The spacing of contour lines indicates the gradient: closely spaced lines mean steep change, widely spaced lines mean a flat region.
Questions This Plot Answers
- How does Z change as a function of X and Y?
Why It Matters
For univariate data, a run sequence plot and a histogram are necessary first steps. For two-dimensional data, a scatter plot fills that role. In a similar manner, three-dimensional data should be plotted. Small data sets from designed experiments can typically be represented by block plots, DOE mean plots, and the like. For large data sets, a contour plot or a 3-D surface plot should be considered a necessary first step in understanding the data. A specialized DOE contour plot variant exists for full and fractional factorial designs.
When to Use a Contour Plot
Use a contour plot when visualizing three-dimensional data on a two-dimensional display. For large data sets, a contour plot or a 3-D surface plot should be considered a necessary first step in understanding the data. Contour plots are also the primary tool for identifying optimal operating conditions in response surface methodology and designed experiments, and are widely used in process optimization, formulation studies, and engineering design.
How to Interpret a Contour Plot
The horizontal axis represents one independent variable and the vertical axis represents the other; the contour lines represent iso-response values of the third variable. Closely spaced contour lines indicate a steep surface where small changes in the inputs produce large changes in the response. Widely spaced lines indicate a flat region. Elliptical contours suggest a well-defined optimum, while saddle-shaped contours indicate a minimax point. The direction of steepest ascent is perpendicular to the contour lines. Color fills between contours, when used, provide an additional visual cue for the response magnitude.
Assumptions and Limitations
The independent variables are usually restricted to a regular grid. If the data do not form a regular grid, a 2-D interpolation is typically needed to produce one. The smoothness and accuracy of the contour lines depend on the quality of the underlying model or interpolation. An additional variable may be required to specify the values for drawing the iso-lines; some software packages require explicit values while others determine them automatically. Extrapolation beyond the range of the observed data should be avoided, as the contour shape may change dramatically outside that region.
Reference: NIST/SEMATECH e-Handbook of Statistical Methods, Section 1.3.3.10
Python Example
import numpy as npimport matplotlib.pyplot as plt
# Generate 2D bivariate normal density surfacex = np.linspace(-3, 3, 200)y = np.linspace(-3, 3, 200)X, Y = np.meshgrid(x, y)
# Bivariate normal with correlation rho = 0.5rho = 0.5Z = (1 / (2 * np.pi * np.sqrt(1 - rho**2))) * np.exp( -1 / (2 * (1 - rho**2)) * (X**2 - 2 * rho * X * Y + Y**2))
fig, ax = plt.subplots(figsize=(8, 6))cf = ax.contourf(X, Y, Z, levels=15, cmap='viridis')ax.contour(X, Y, Z, levels=15, colors='white', linewidths=0.5, alpha=0.4)plt.colorbar(cf, ax=ax, label='Density')ax.set_xlabel("X")ax.set_ylabel("Y")ax.set_title("Contour Plot — Bivariate Normal Density (rho=0.5)")ax.set_aspect('equal')plt.tight_layout()plt.show()