This is documentation for Mathematica 3, which was
based on an earlier version of the Wolfram Language.
View current documentation (Version 11.2)
 Documentation / Mathematica / The Mathematica Book / Principles of Mathematica / The Structure of Graphics and Sound  /

2.9.7 Density and Contour Plots

Graphics objects that represent density and contour plots.

The functions DensityPlot and ContourPlot discussed in Section 1.9.6 work by creating ContourGraphics and DensityGraphics objects containing arrays of values.
Most of the options for density and contour plots are the same as those for ordinary two-dimensional plots. There are, however, a few additional options.

Additional options for density plots.

In a density plot, the color of each cell represents its value. By default, each cell is assigned a gray level, running from black to white as the value of the cell increases. In general, however, you can specify other "color maps" for the relation between the value of a cell and its color. The option ColorFunction allows you to specify a function which is applied to each cell value to find the color of the cell. The cell values are scaled so as to run between 0 and 1 in a particular density plot. The function you give as the setting for ColorFunction may return any Mathematica color directive, such as GrayLevel, Hue or RGBColor. A common setting to use is ColorFunction->Hue.

  • Here is a density plot with the default ColorFunction.
  • In[1]:= DensityPlot[Sin[x y], {x, -1, 1}, {y, -1, 1}]

  • This gives a density plot with a different "color map".
  • In[2]:= Show[%, ColorFunction -> (GrayLevel[#^3]&)]

    Options for contour plots.

    In constructing a contour plot, the first issue is what contours to use. With the default setting Contours->10, Mathematica uses a sequence of 10 contour levels equally spaced between the minimum and maximum values defined by the PlotRange option.

    Specifying contours.

  • This creates a contour plot with two contours.
  • In[3]:= ContourPlot[Sin[x y], {x, -1, 1}, {y, -1, 1},
    Contours -> {-.5, .5}]

    There are some slight subtleties associated with labeling density and contour plots. Both the Axes and Frame options from ordinary two-dimensional graphics can be used. But setting AxesOrigin->Automatic keeps the axes outside the plot in both cases.