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 / Practical Introduction / Graphics and Sound  /

1.9.6 Contour and Density Plots

Contour and density plots.

  • This gives a contour plot of the function

  • In[1]:= ContourPlot[Sin[x] Sin[y], {x, -2, 2}, {y, -2, 2}]

    A contour plot gives you essentially a "topographic map" of a function. The contours join points on the surface that have the same height. The default is to have contours corresponding to a sequence of equally spaced z values. Contour plots produced by Mathematica are by default shaded, in such a way that regions with higher z values are lighter.

    Some options for ContourPlot. The first set can also be used in Show.

  • Particularly if you use a display or printer that does not handle gray levels well, you may find it better to switch off shading in contour plots.
  • In[2]:= Show[%, ContourShading -> False]

    You should realize that if you do not evaluate your function on a fine enough grid, there may be inaccuracies in your contour plot. One point to notice is that whereas a curve generated by Plot may be inaccurate if your function varies too quickly in a particular region, the shape of contours can be inaccurate if your function varies too slowly. A rapidly varying function gives a regular pattern of contours, but a function that is almost flat can give irregular contours. You can typically overcome such problems by increasing the value of PlotPoints.

  • Density plots show the values of your function at a regular array of points. Lighter regions are higher.
  • In[3]:= DensityPlot[Sin[x] Sin[y], {x, -2, 2}, {y, -2, 2}]

  • You can get rid of the mesh like this. But unless you have a very large number of regions, plots usually look better when you include the mesh.
  • In[4]:= Show[%, Mesh -> False]

    Some options for DensityPlot. The first set can also be used in Show.