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


enter image description here

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


enter image description here

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)?

Thanks in advance.


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}}]

enter image description here

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.

  • 1
    $\begingroup$ "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. $\endgroup$ – J. M. will be back soon Aug 14 '17 at 5:58
  • $\begingroup$ @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. $\endgroup$ – Mr.Wizard Aug 14 '17 at 6:03
  • $\begingroup$ 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! $\endgroup$ – Rain Aug 23 '17 at 20:29
  • $\begingroup$ @Rain No problem, glad I could help, and thanks for the Accept. :-) $\endgroup$ – Mr.Wizard Aug 24 '17 at 0:42

For this, use PlotRangePadding

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

enter image description here

  • $\begingroup$ 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? $\endgroup$ – Rain Aug 13 '17 at 23:51
  • 1
    $\begingroup$ As the Wizard notes in his answer, @Rain, use Scaled[] for "relative" positions or dimensions in general. $\endgroup$ – J. M. will be back soon Aug 14 '17 at 6:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.