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.
In[1]:=
Click for copyable input
Out[1]=
option name
default value
ColorFunctionAutomaticwhat colors to use for shading; Hue uses a sequence of hues
MeshNonewhether to draw a mesh
PlotPointsAutomaticnumber of initial sample points in each direction
MaxRecursionAutomaticthe maximum number of recursive subdivision steps to do

Some options for DensityPlot.

You can include a mesh like this.
In[2]:=
Click for copyable input
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 Wolfram Language color directive, such as GrayLevel, Hue, or RGBColor. A common setting to use is ColorFunction->Hue.

This uses different hues to represent different values.
In[3]:=
Click for copyable input
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.
In[4]:=
Click for copyable input
Out[4]=
This DensityPlot is identical to the one above, but uses the gradient.
In[5]:=
Click for copyable input
Out[5]=
This gives a contour plot of the function.
In[6]:=
Click for copyable input
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 the Wolfram Language are by default shaded, in such a way that regions with higher z values are lighter.

option name
default value
ColorFunctionAutomaticwhat colors to use for shading; Hue uses a sequence of hues
ContoursAutomaticthe 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
ContourShadingAutomatichow to shade the regions; None leaves the regions blank, or a list of colors can be provided
PlotPointsAutomaticnumber of initial sample points in each direction
MaxRecursionAutomaticthe maximum number of recursive subdivision steps to do

Some options for ContourPlot.

This shows the plot with no shading.
In[7]:=
Click for copyable input
Out[7]=
This cycles the colors used for contour regions between light red and light purple.
In[8]:=
Click for copyable input
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.