# Why does PlotRange remove the top and bottom of a DensityPlot's white frame?

The following code (from the Mathematica documentation) produces a simple density graph:

DensityPlot[Sin[x]Sin[y],{x,-4,4},{y,-3,3}]


If I add PlotRange to the code, the top and bottom white margins (but not the left and right ones) disappear:

DensityPlot[Sin[x]Sin[y],{x,-4,4},{y,-3,3},PlotRange->{-16,16}]


Why does this happen, and how can I avoid it while still using PlotRange (or some other way of limiting my density graph's value range)?

I don't understand why this happens but for some reason specifying single (Z) PlotRange like PlotRange->{-16,16} causes the default option value:

PlotRangePadding -> {{Scaled[0.02], Scaled[0.02]}, {Scaled[0.02], Scaled[0.02]}}


To change to:

PlotRangePadding -> {{Scaled[0.02], Scaled[0.02]}, {0., 0.}}


Which causes the top and bottom padding to disappear. You can get it back by explicitly specifying PlotRangePadding -> Scaled[0.02].

Inadvertently I also found that providing the full {X, Y, Z} specification does not suffer from this problem, e.g.

DensityPlot[Sin[x] Sin[y], {x, -4, 4}, {y, -3, 3},
PlotRange -> {Full, Full, {-0.6, 0.4}}]


If this is not what you want perhaps look at ColorFunctionScaling and ColorFunction, or describe what you want again and I'll try to help.

• "PlotRange -> {-16, 16} is not a valid specification as far as I can tell" - in the case of functions like ContourPlot[] and DensityPlot[], it means that all the $z$-values in that range should be displayed; of course, since $\sin$ only takes values in $[-1,1]$ for real arguments, it does nothing. Why that mucks up PlotRangePadding as well is nevertheless interesting. Commented Aug 14, 2017 at 5:58
• @J.M. Sorry, you're right of course, for some reason I was thinking the full three part specification was necessary. That makes this odd padding behavior even more mysterious as the full specification doesn't suffer from it. I'll try to correct my answer now. Commented Aug 14, 2017 at 6:03
• Sorry for the late reply. Been really busy. @mr-wizard, your answer is what I was looking for (minus the reason for the mysterious overwriting of PlotRangePadding due to PlotRange, but that was the least important bit of my question). Thanks a lot!
– Rain
Commented Aug 23, 2017 at 20:29
• @Rain No problem, glad I could help, and thanks for the Accept. :-) Commented Aug 24, 2017 at 0:42

For this, use PlotRangePadding

DensityPlot[Sin[x] Sin[y], {x, -4, 4}, {y, -3, 3}, PlotRange -> {{-4, 4}, {-3, 3}},

• Thanks. The documentation says that the default padding value is 2%. Is there a way to tell Mathematica to use relative padding? In other words, can I enforce the 2% padding without calculating what 2% of my axis range is (e.g. 0.12 for a range of -3 to 3)? Also, for bonus points, does anyone know why PlotRange gets rid of the padding in the first place?
• As the Wizard notes in his answer, @Rain, use Scaled[] for "relative" positions or dimensions in general. Commented Aug 14, 2017 at 6:07