Density and Contour Plots

Density and contour plots.

This gives a density plot of sin(x) sin(y). Lighter regions show higher values of the function:

DensityPlot[Sin[x] Sin[y],{x,-2,2},{y,-2,2}]

Some options for (RefLink[DensityPlot, paclet:ref/DensityPlot]).

You can include a mesh like this:

DensityPlot[Sin[x] Sin[y],{x,-2,2},{y,-2,2},Mesh->19]

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 (RefLink[ColorFunction, paclet:ref/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 (RefLink[GrayLevel, paclet:ref/GrayLevel]), (RefLink[Hue, paclet:ref/Hue]), or (RefLink[RGBColor, paclet:ref/RGBColor]). A common setting to use is (RefLink[ColorFunction, paclet:ref/ColorFunction])->(RefLink[Hue, paclet:ref/Hue]).

This uses different hues to represent different values:

DensityPlot[Sin[x] Sin[y],{x,-2,2},{y,-2,2},ColorFunction->Hue]

A significant resource for customized color functions is the (RefLink[ColorData, paclet:ref/ColorData]) function. (RefLink[ColorData, paclet:ref/ColorData]) provides many customized sets of colors which can be used directly by (RefLink[ColorFunction, paclet:ref/ColorFunction]).

This shows a list of the gradients which can be accessed using (RefLink[ColorData, paclet:ref/ColorData]):

ColorData["Gradients"]

This (RefLink[DensityPlot, paclet:ref/DensityPlot]) is identical to the one above, but uses the "SolarColors" gradient:

DensityPlot[Sin[x] Sin[y],{x,-2,2},{y,-2,2},ColorFunction->ColorData["SolarColors"]]

This gives a contour plot of the function:

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 the Wolfram Language are by default shaded, in such a way that regions with higher z values are lighter.

Some options for (RefLink[ContourPlot, paclet:ref/ContourPlot]).

This shows the plot with no shading:

ContourPlot[Sin[x] Sin[y],{x,-2,2},{y,-2,2},ContourShading->None]

This cycles the colors used for contour regions between light red and light purple:

ContourPlot[Sin[x] Sin[y],{x,-2,2},{y,-2,2},ContourShading->{LightRed,LightPurple}]

Both (RefLink[DensityPlot, paclet:ref/DensityPlot]) and (RefLink[ContourPlot, paclet:ref/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 (RefLink[PlotPoints, paclet:ref/PlotPoints]) and (RefLink[MaxRecursion, paclet:ref/MaxRecursion]) options.

One point to notice is that whereas a curve generated by (RefLink[Plot, paclet:ref/Plot]) may be inaccurate if your function varies too quickly in a particular region, the shape of contours generated by (RefLink[ContourPlot, paclet:ref/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 (RefLink[PlotPoints, paclet:ref/PlotPoints]) or (RefLink[MaxRecursion, paclet:ref/MaxRecursion]).