4
$\begingroup$

I'd need to compare two different DensityPlot by keeping the PlotRange fixed: however I'm not able to do that because DensityPlot automatically fix the range of the plot to the {min,max} of the function. I've been reading something about it, a tried some possible solutions (for example ColorFunctionScaling -> False) but non of them solved my problem.

Here is part of the code I'm using:

DensityPlot[(
 E^(-x^2 - y^2) (-1 - 4 x y))/Sqrt[\[Pi]], {x, -4, 4}, {y, -4, 4},
 PlotRange -> {Automatic, Automatic, {-0.69, 0.69}},
 PlotPoints -> 150,
 ColorFunction -> (Blend[{RGBColor[0.02, 1, 1], RGBColor[0, 0.48, 1], 
      RGBColor[0, 0, 0.73], Black, RGBColor[0.6, 0.22, 0], RGBColor[
      1, 0.55, 0], White}, #1] &),
 PlotLegends -> 
  Placed[BarLegend[Automatic, LegendMarkerSize -> 250], Right],
 ImageSize -> 300,
 Background -> Transparent
 ]
DensityPlot[(
 E^(-x^2 - y^2) (2 x - 2 y))/Sqrt[\[Pi]], {x, -4, 4}, {y, -4, 4},
 PlotRange -> {Automatic, Automatic, {-0.69, 0.69}},
 PlotPoints -> 150,
 ColorFunction -> (Blend[{RGBColor[0.02, 1, 1], RGBColor[0, 0.48, 1], 
      RGBColor[0, 0, 0.73], Black, RGBColor[0.6, 0.22, 0], RGBColor[
      1, 0.55, 0], White}, #1] &),
 PlotLegends -> 
  Placed[BarLegend[Automatic, LegendMarkerSize -> 250], Right],
 ImageSize -> 300,
 Background -> Transparent
 ]

and the output is the following:

enter image description here

enter image description here

where obviously the range of the plots is not -0.69:0.69 in both of them. Using the correct color scaling the orange/brown background in the first picture would be black as in the second one.

Any solution for fixing it? Edit: I think the solution lies in using ColorData function in some way: I noticed that, with a standard color function (e.g. "Heat") I can use the combination:

ColorFunctionScaling -> False,
ColorFunction -> ColorData[{"Heat", {-0.69, 0.69}}],

but I'm not able to reproduce it with my custom ColorFunction...

$\endgroup$
5
$\begingroup$

To compare the two plots you can rescale the first plot (f1) to the range of the second one:

f1 = (E^(-x^2 - y^2) (-1 - 4 x y)) / Sqrt[Pi];
f2 = (E^(-x^2 - y^2) (2 x - 2 y)) / Sqrt[Pi];

zr = Through[{NMinValue, NMaxValue}[{f2, -4 <= x <= 4}, {x, y}]]

{-0.684397, 0.684397}

col = 
   {RGBColor[0.02, 1, 1], RGBColor[0, 0.48, 1], RGBColor[0, 0, 0.73], Black, 
    RGBColor[0.6, 0.22, 0], RGBColor[1, 0.55, 0], White};

Grid[{{
   DensityPlot[f1, {x, -4, 4}, {y, -4, 4},
    PlotRange -> zr,
    ColorFunction -> (Blend[col, Rescale[#, zr]] &),
    ColorFunctionScaling -> False,
    ImageSize -> 300],

   DensityPlot[f2, {x, -4, 4}, {y, -4, 4},
    PlotRange -> zr,
    ColorFunction -> (Blend[col, #] &),
    ImageSize -> 300,
    PlotLegends -> Automatic]
   }}]

enter image description here

$\endgroup$
  • $\begingroup$ Thanks for that, it works perfectly! $\endgroup$ – Fraccalo Aug 22 '17 at 17:32
2
$\begingroup$

This problem can be solved by using ListDensityPlot and Blend.

First you have to obtain the discrete values of your functions:

f1 = Table[(E^(-x^2 - y^2) (-1 - 4 x y))/Sqrt[Pi], {x, -4, 4, 
0.2}, {y, -4, 4, 0.2}];
f2 = Table[(E^(-x^2 - y^2) (2 x - 2 y))/Sqrt[Pi], {x, -4, 4, 
0.2}, {y, -4, 4, 0.2}];

Second, using Blend function to map a given color range to your defined color scheme:

ColorFunction -> (Blend[
Transpose@{Subdivide[-0.684397, 0.684397, 6], 
  colormap}, #1] &), ColorFunctionScaling -> False

notice that Subdivide[-0.684397, 0.684397, 6] is a list of the same length of your color scheme, and -0.684397, 0.684397 are the lowest and highest limits of the wanted color range (your fixed PlotRange).

So now this problem is settled already. The full codes and results are listed below

    f1 = Table[(E^(-x^2 - y^2) (-1 - 4 x y))/Sqrt[Pi], {x, -4, 4, 
    0.2}, {y, -4, 4, 0.2}];
    f2 = Table[(E^(-x^2 - y^2) (2 x - 2 y))/Sqrt[Pi], {x, -4, 4, 
    0.2}, {y, -4, 4, 0.2}];
    colormap = {RGBColor[0.02, 1, 1], RGBColor[0, 0.48, 1], 
    RGBColor[0, 0, 0.73], Black, RGBColor[0.6, 0.22, 0], RGBColor[1, 0.55, 0], White};
    options = {ColorFunction -> (Blend[
       Transpose@{Subdivide[-0.684397, 0.684397, 6], 
         colormap}, #1] &), ColorFunctionScaling -> False, 
   InterpolationOrder -> 3, PlotRange -> All, ImageSize -> 200};
p1 = ListDensityPlot[f1, options];
p2 = ListDensityPlot[f2, options, PlotLegends -> Automatic];
Grid[{{p1, p2}}]

enter image description here

$\endgroup$

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.