Contour and Density Plots

A contour graph (sometimes called a level curve graph) of a function f(x,y) is a graph of curves in the xy-plane of the form k = f(x,y) for various constant values of k . For example, weather maps often show isobaric (constant pressure) or isothermal (constant temperature) curves, which are the level curves for the barametric pressure or temperature function, respectively. In Mathematica, contour graphs are produced with the ContourPlot function.

If we entered the command

ContourPlot[ x^3 - 3 y^2 x, {x,-1,1}, {y,-1,1} ]

a contour plot of the previous graphic would be produced. However, because the information stored by Mathematica for Plot3D (which generates what Mathematica calls a SurfaceGraphic object) and ContourPlot (which generates a ContourGraphic object) are so similar, we can actually convert directly from one form to the other, thereby saving having to recompute all the sample values of the function. The following command does this, and produces the exact same result (only faster) as if you had entered the ContourPlot command above.

[Graphics:3dgraphinggr2.gif][Graphics:3dgraphinggr22.gif]
[Graphics:3dgraphinggr2.gif][Graphics:3dgraphinggr23.gif]
[Graphics:3dgraphinggr2.gif][Graphics:3dgraphinggr24.gif]

In addition to producing several level curves for the function, Mathematica also uses shading to indicate the relative z-values. Compare the shading used in the contour plot with the actual graph to see how the shading works.

Of course, the ContourPlot function has many options which could be viewed with the Options[ContourPlot] command, but usually the default settings are adequate. If you need to change the number of contours which are graphed, that can be done with the Contours option (the default is 10). You can also insist that Mathematica try to smooth the contours by setting ContourSmoothing -> Automatic (the default is None so that no smoothing of curves is attempted). Be warned, though, that smoothing contours can take both time and memory, so you should have plenty of both before attempting smoothing.

A density graph of a function of two variables is similar to a contour graph. Shading which indicates the relative z-values is done in rectangular regions in the xy-plane, but no contour curves are drawn. So a density graph can be computed more quickly than a contour graph. The following command converts the SurfaceGraphics object stored in the variable g to a DensityGraphics object. As with the above discussion on contour plotting, the same result could be obtained using the DensityPlot command, except that all the values of the sample points would have to be recomputed.

[Graphics:3dgraphinggr2.gif][Graphics:3dgraphinggr25.gif]
[Graphics:3dgraphinggr2.gif][Graphics:3dgraphinggr26.gif]
[Graphics:3dgraphinggr2.gif][Graphics:3dgraphinggr27.gif]