4
$\begingroup$
DensityPlot[
 Max[0, 0.1 - Abs[1. - Sqrt[x^2 + y^2]]],
 {x, -1.1, 1.1}, {y, -1.1, 1.1},
 ColorFunction -> (Opacity[#, Blue] &), Frame -> False]

This prints out nicely (well, almost...):

enter image description here

But a slight modification of the plot range is enough to change the result drastically:

DensityPlot[
 Max[0, 0.1 - Abs[1. - Sqrt[x^2 + y^2]]],
 {x, -1.2, 1.2}, {y, -1.2, 1.2},
 ColorFunction -> (Opacity[#, Blue] &), Frame -> False]

enter image description here

This effect seems to be closely tied to this particular data. A change in parameters or the formula makes the ring filled again.

DensityPlot[
 Max[0, 0.1 - Abs[1.3 - Sqrt[x^2 + y^2]]],
 {x, -1.2, 1.2}, {y, -1.2, 1.2},
 ColorFunction -> (Opacity[#, Blue] &), Frame -> False]

DensityPlot[
 Max[0, Abs[1. - Sqrt[x^2 + y^2]]],
 {x, -1.2, 1.2}, {y, -1.2, 1.2}, 
 ColorFunction -> (Opacity[#, Blue] &), Frame -> False]

Both of the above print out as expected.

Why does this happen? How to prevent it?

$\endgroup$
5
$\begingroup$

It appears that PlotRange must be specified to assure that the Full range of data is displayed in the second DensityPlot. Also, specifying PlotPoints improves the appearance of the first DensityPlot.

DensityPlot[Max[0, 0.1 - Abs[1. - Sqrt[x^2 + y^2]]], {x, -1.1, 1.1}, {y, -1.1, 1.1}, 
ColorFunction -> (Opacity[#, Blue] &), PlotPoints -> 100, Frame -> False, PlotRange -> Full]

enter image description here

DensityPlot[Max[0, 0.1 - Abs[1. - Sqrt[x^2 + y^2]]], {x, -1.2, 1.2}, {y, -1.2, 1.2}, 
ColorFunction -> (Opacity[#, Blue] &), PlotPoints -> 100, Frame -> False, PlotRange -> Full]

enter image description here

Update - Corresponding 1-D behavior

Plot exhibits similar behavior

Plot[Max[0, 0.1 - Abs[1. - Sqrt[x^2]]], {x, -1.2, 1.2}]

enter image description here

omits part of the curve unless PlotPoints is set to 100 or more. And,

Plot[Max[0, 0.1 - Abs[1. - Sqrt[x^2]]], {x, -1.1, 1.1}]

enter image description here

has far too small a PlotRange, unless it is set explicitly to All or Full. This same behavior persists for ranges as large as about {x, -1.14, 1.14}, and as small as about {x, -1.09, 1.09}. It is not uncommon that Mathematica plotting routines have difficulty with functions that are zero except in narrow ranges.

$\endgroup$
  • $\begingroup$ All right, thanks. But... why...? Why do I have to specify PlotRange since I've already specified the range with {x, -1.2, 1.2}, {y, -1.2, 1.2} and obviously the whole ring is within that range? $\endgroup$ – gaazkam Feb 13 '15 at 16:37
  • $\begingroup$ @gaazkam The problem is not with PlotRange for x and y but for the function you were plotting. Plot has this problem too. I shall add a bit more to my answer. $\endgroup$ – bbgodfrey Feb 13 '15 at 20:43

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.