# Why does PlotRange in DensityPlot have no effect

It looks like DensityPlot and ListDensityPlot automatically clip the plot range to the min and max value of the data, when PlotRange is set to be larger.

For example:

When the plot range is smaller than the range of the data PlotRange works

DensityPlot[Sin[x] Sin[y], {x, -4, 4}, {y, -3, 3},
PlotRange -> {Automatic, Automatic, {-0.5, 0.5}},
PlotLegends -> Automatic] but when the plot range is larger than the range of the data, it has no effects

DensityPlot[Sin[x] Sin[y], {x, -4, 4}, {y, -3, 3},
PlotRange -> {Automatic, Automatic, {-2, 2}},
PlotLegends -> Automatic] PlotRange in 3D and 1D behaves more like what we expected:

Plot3D[Sqrt[1 - x^2 - y^2], {x, -2, 2}, {y, -2, 2},
PlotRange -> {Automatic, Automatic, {0, 2}}] Plot[Sin[x], {x, 0, 6 Pi}, PlotRange -> {Automatic, {-2, 2}}] So why does PlotRange in DensityPlot and ListDensityPlot behave like this? It looks inconsistent to me.

• I believe it is always automatically adapting the full color scale to the full function range. The first case SEEMS to work but what you're really modifying is the function range that the colorfunction can "see" – Dr. belisarius May 8 '15 at 20:54
• @belisarius OK that makes sense. Thanks ! – xslittlegrass May 9 '15 at 1:31

## 2 Answers

As others mentioned, this is really an effect of ColorFunctionScaling -> True

Manipulate[
DensityPlot[Sin[x] Sin[y], {x, -4, 4}, {y, -3, 3},
PlotRange -> {Automatic, Automatic, {-k, k}},
PlotLegends -> BarLegend[{Automatic, {-k, k}}],
ColorFunctionScaling -> False,
ColorFunction -> (ColorData["Rainbow"][Rescale[#, {-k, k}]] &),
PlotPoints -> 30], {k, 1, 10}] I don't see how the density plot should react to a PlotRange wider than the $z$ data range.

I think that the clipping you might be referring to happens in two other places:

1. The ColorFunctionScaling is True by default, so in this case ColorFunction is passed $z$ scaled to lie in the $(0,1)$ interval. The values considered for the rescaling are those function values that lie within the PlotRange. If your manual PlotRange clips the function, then this setting has an effect, but if your manual PlotRangeincludes all function values, then the function's values themselves determine the scaling, and PlotRange becomes irrelevant.

2. The automatically generated BarLegend is also given the $(z_{min},\ z_{max})$ values to work with. You can change that by hand (e.g. PlotLegends->BarLegend[{Automatic, {-10, 20}}]) , but it won't influence the coloring in the plot.