Mathematica 9 is now available
Previous section-----Next section

1.9.5 Contour and Density Plots

ContourPlot[f, {x,  ,  }, {y,  ,  }]
make a contour plot of f as a function of x and y
DensityPlot[f, {x,  ,  }, {y,  ,  }]
make a density plot of f

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}]

Out[1]=

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.

option name default value
ColorFunction Automatic what colors to use for shading; Hue uses a sequence of hues
Contours 10 the total number of contours, or the list of z values for contours
PlotRange Automatic the range of values to be included; you can specify { ,  }, All or Automatic
ContourShading True whether to use shading
PlotPoints 25 number of evaluation points in each direction
Compiled True whether to compile the function being plotted

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]

Out[2]=

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}]

Out[3]=

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]

Out[4]=

option name default value
ColorFunction Automatic what colors to use for shading; Hue uses a sequence of hues
Mesh True whether to draw a mesh
PlotPoints 25 number of evaluation points in each direction
Compiled True whether to compile the function being plotted

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


Any questions about topics on this page? Click here to get an individual response.Buy NowMore Information
THIS IS DOCUMENTATION FOR AN OBSOLETE PRODUCT.
SEE THE DOCUMENTATION CENTER FOR THE LATEST INFORMATION.