3
$\begingroup$

In a specified ColorFunction such as SunsetColors, how can I change the color values for a DensityPlot such that I get the sunset color for the negative values only, black for $0$, and purple color for the positive values? My function is $b = e^{-n x^2 - y^2} + n Sin[y]$. I have to make several density plots against $x$ and $y$ by taking different values of $n$. Each time I change $n$, my PlotLegends change. Is there a way to have a common PlotLegend for the following three plots? Thank you.

b = Exp[-n x^2 - y^2] + n Sin[y];
b0 = b /. n -> 0;
DensityPlot[b0, {x, -4, 4}, {y, -4, 4}, 
 ColorFunction -> "SunsetColors", PlotLegends -> Automatic];
b1 = b /. n -> 1;
DensityPlot[b1, {x, -4, 4}, {y, -4, 4}, 
 ColorFunction -> "SunsetColors", PlotLegends -> Automatic];
b2 = b /. n -> 2;
DensityPlot[b2, {x, -4, 4}, {y, -4, 4}, 
 ColorFunction -> "SunsetColors", PlotLegends -> Automatic];
$\endgroup$

2 Answers 2

3
$\begingroup$
$Version

(* "13.1.0 for Mac OS X x86 (64-bit) (June 16, 2022)" *)

Clear["Global`*"]

b = Exp[-n x^2 - y^2] + n Sin[y];

The range of the common legend is

{min, max} =
 #[{b, 0 <= n <= 2, -4 <= x <= 4, -4 <= y <= 4}, {n, x, y}] & /@
   {NMinValue, NMaxValue} // Rationalize

(* {-2, 2} *)

The common color function is

cf[x_] = Piecewise[{
    {Purple, x > 0},
    {Black, x == 0},
    {ColorData["SunsetColors"][-x/2], x < 0}}];

Legended[
 Column[DensityPlot[b /. n -> #, {x, -4, 4}, {y, -4, 4},
     PlotPoints -> 100,
     MaxRecursion -> 5,
     ColorFunction -> cf,
     ColorFunctionScaling -> False,
     PlotLabel -> StringForm["n = ``", #]] & /@
   Range[0, 2]],
 BarLegend[{cf, {min, max}},
  LegendMarkerSize -> 500]]

enter image description here

Alternatively, for n varying on reals

Manipulate[
 DensityPlot[b /. n -> nv, {x, -4, 4}, {y, -4, 4},
  PlotPoints -> 100,
  MaxRecursion -> 5,
  ColorFunction -> cf,
  ColorFunctionScaling -> False,
  PlotLegends -> BarLegend[{cf, {min, max}}]],
 {{nv, 1, n}, 0, 2, 0.01, Appearance -> "Labeled"},
 SynchronousUpdating -> False,
 TrackedSymbols :> True]

enter image description here

$\endgroup$
4
$\begingroup$

The easiest way to have a unified color scale is to set ColorFunctionScaling to False, and implement a custom scaling for the color function. For this, we can use ColorData to get the underlying color function, and Rescale to do the rescaling:

cf = ColorData["SunsetColors"][Rescale[#, {2, -3}]] &;

b[n_] := Exp[-n x^2 - y^2] + n Sin[y];
DensityPlot[b[0], {x, -4, 4}, {y, -4, 4}, ColorFunction -> cf, 
 ColorFunctionScaling -> False, PlotLegends -> Automatic]
DensityPlot[b[1], {x, -4, 4}, {y, -4, 4}, ColorFunction -> cf, 
 ColorFunctionScaling -> False, PlotLegends -> Automatic]
DensityPlot[b[2], {x, -4, 4}, {y, -4, 4}, ColorFunction -> cf, 
 ColorFunctionScaling -> False, PlotLegends -> Automatic]

enter image description here

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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