This question already has an answer here:

I have a density plot as below:-

v = {2, 3};
w = {4, 6};
DensityPlot[Exp[-w.Abs[v - {x, y}]], {x, 0, 5}, {y, 0, 7}]

enter image description here

As you can see, the boundary of the white area is so ragged, while theoretically speaking it should be smooth (that can be expected, as you can see a smooth plot when you try to remove the Exp from the function).

I tried to fix it by applying a higher WorkingPrecision in DensityPlot, but it doesn't help. Are there any other ways to increase the density of the plot's grid?

I don't want to remove the Exp from the function. Is it possible to add Exp to the color scale mapping instead?

Or there are other ways to fix?

Many thanks!


marked as duplicate by J. M. is computer-less plotting Oct 21 '18 at 15:22

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • 2
    $\begingroup$ More PlotPoints? $\endgroup$ – Henrik Schumacher Jun 16 '18 at 20:41
  • $\begingroup$ DensityPlot[Exp[-w.Abs[v - {x, y}]], {x, 0, 5}, {y, 0, 7}, MaxRecursion -> 5, PlotPoints -> 150] $\endgroup$ – Bob Hanlon Jun 16 '18 at 20:56
  • 2
    $\begingroup$ Also, additionally to what others commented PlotRange->All can help getting rid of the clipped white area in the middle. $\endgroup$ – Thies Heidecke Jun 16 '18 at 21:17
  • $\begingroup$ Thanks for all your help. @ThiesHeidecke May I ask why can PlotRange->All get rid of that? $\endgroup$ – H42 Jun 16 '18 at 22:07
  • $\begingroup$ @HMC Sure! Usually Mathematica uses a heuristic to determine what is a useful value range to plot as opposed to plot the full range of all assumed values (think a function with singularities like 1/(x-1) and how it would give a useless plot by trying to plot the infinite range). In some cases the heuristic does cut off interesting parts of the function that were actually useful and not infinite, such as in your functions case. In the case of DensityPlot that means the values get clamped before they are fed to the ColorFunction giving the overexposed clamped look. (1/2) $\endgroup$ – Thies Heidecke Jun 16 '18 at 22:14

The white part of your plot is a peak that mathematica cuts off unless PlotRange -> All is added.

Transforming the color map as you mention works.
To do that we need to extract the range of the function:

lims = {};
With[{v = {2, 3}, w = {4, 6}},
  DensityPlot[Exp[-w.Abs[v - {x, y}]], {x, 0, 5}, {y, 0, 7},
               ColorFunction -> ((lims = MinMax[{lims, #}]; 0) &),
                ColorFunctionScaling -> False, PlotRange -> All];

{-36., -0.14285714}

With[{v = {2, 3}, w = {4, 6}, cols = ColorData[ColorData["Gradients"][[6]]]},
  DensityPlot[Exp[-w.Abs[v - {x, y}]], {x, 0, 5}, {y, 0, 7},
               ColorFunction -> (cols[Rescale[Log[#], Log[lims]]] &),
                ColorFunctionScaling -> False, PlotRange -> All]]
  • $\begingroup$ Thanks. May I ask what lims and ((lims = MinMax[{lims, #}]; 0) &) are doing? $\endgroup$ – H42 Jun 16 '18 at 22:06

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