MATHEMATICA TUTORIAL

# Density and Contour Plots

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

Density and contour plots.

This gives a density plot of . Lighter regions show higher values of the function.
 Out[1]=
 option name default value ColorFunction Automatic what colors to use for shading; Hue uses a sequence of hues Mesh None whether to draw a mesh PlotPoints Automatic number of initial sample points in each direction MaxRecursion Automatic the maximum number of recursive subdivision steps to do

Some options for DensityPlot.

You can include a mesh like this.
 Out[2]=

In a density plot, the color of each point represents the value at that point of the function being plotted. By default, the color ranges from black to white through intermediate shades of blue as the value of the function increases. In general, however, you can specify other "color maps" for the relation between the value at a point and its color. The option ColorFunction allows you to specify a function which is applied to the function value to find the color at any point. The color function may return any Mathematica color directive, such as GrayLevel, Hue, or RGBColor. A common setting to use is .

This uses different hues to represent different values.
 Out[3]=

A significant resource for customized color functions is the ColorData function. ColorData provides many customized sets of colors which can be used directly by ColorFunction.

This shows a list of the gradients which can be accessed using ColorData.
 Out[4]=
This DensityPlot is identical to the one above, but uses the gradient.
 Out[5]=
This gives a contour plot of the function.
 Out[6]=

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 Automatic the total number of contours, or the list of z values for contours PlotRange {Full,Full,Automatic} the range of values to be included; you can specify , All or Automatic, or a list ContourShading Automatic how to shade the regions; None leaves the regions blank, or a list of colors can be provided PlotPoints Automatic number of initial sample points in each direction MaxRecursion Automatic the maximum number of recursive subdivision steps to do

Some options for ContourPlot.

This shows the plot with no shading.
 Out[7]=
This cycles the colors used for contour regions between light red and light purple.
 Out[8]=

Both DensityPlot and ContourPlot use an adaptive algorithm that subdivides parts of the plot region to obtain more sample points for a smoother representation of the function you are plotting. Because the number of sample points is always finite, however, it is possible that features of your function will sometimes be missed. When necessary, you can increase the number of sample points by increasing the values of the PlotPoints and MaxRecursion options.

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 generated by ContourPlot 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 this by increasing the value of PlotPoints or MaxRecursion.

## Tutorial CollectionTutorial Collection

New to Mathematica? Find your learning path »
Have a question? Ask support »