7
$\begingroup$

I'm doing density plots in Mathematica where I mostly plot Wigner functions.

The main objective when plotting a Wigner function is to demonstrate that the underlying state is nonclassical, as indicated by negative values in the Wigner function. Therefore I want to make these negative values stand out in the plot.

To demonstrate this, I've plotted the Wigner function of an even cat state

cat state

which was generated using the following code

(* Plots the Wigner function of an even Cat State *)
α = Sqrt[
  16];
Wigner[x_, y_] := 
  1/(π Sqrt[
    1 + Exp[-2 α^2]]) (Exp[-2 (x - α)^2 - 2 y^2] + 
     Exp[-2 (x + α)^2 - 2 y^2] + 
     2 Exp[-2 x^2 - 2 y^2]*Cos[4 y α]);

DensityPlot[Wigner[x, y], {x, -6, 6}, {y, -2, 2},
    PlotRange -> All,
    ColorFunction -> "LightTemperatureMap",
    PlotLegends -> Placed[
    BarLegend[
        {"LightTemperatureMap", {-.5, .5}},
        LegendMargins -> {{26, 20}, {-15, 0}},
        LegendMarkerSize -> {475, 30}],
    Above],
    ImagePadding -> {{45, 20}, {45, 10}},
    PlotRangePadding -> None,
    ImageSize -> {600, 200},
    AspectRatio -> Automatic,
    FrameLabel -> {"x", "y"},
    FrameTicks -> {{{-2, 0, 2}, None}, {Table[-6 + 2 i, {i, 0, 6}], 
    None}},
    FrameStyle -> Black,
    FrameTicksStyle -> Directive[Black, 14],
    LabelStyle -> {Black, Bold, 14},
    PlotPoints -> 50
 ]

As you can see in the figure above, it's difficult to distinguish positive and negative values around the zero value.

All of the color functions that I've found in Mathematica are linear colormaps where small negative values tend to be near the same color as the zero values, and are thus hidden. I'm looking for a nonlinear color function that colors all negative values differently than positive or zero values

See for example the right figure down below wigner colormap

$\endgroup$
  • $\begingroup$ How about something like CoolColorN[ z_ ] := RGBColor[z^3, 1 - z^3, 1]; ContourPlot[Sin[x y], {x, -1, 1}, {y, -1, 1}, ColorFunction -> CoolColor] $\endgroup$ – Lotus Apr 18 '18 at 9:25
  • $\begingroup$ @Lotus I still want better distinction between the positive and negative values, but I think you're on to something. $\endgroup$ – Turbotanten Apr 18 '18 at 9:33
  • 1
    $\begingroup$ I think you should be able to modify the function however you want. $\endgroup$ – Lotus Apr 18 '18 at 9:35
  • 1
    $\begingroup$ I recommend looking up the documentation for ColorFunction, ColorData and ColorFunctionScaling. $\endgroup$ – Sjoerd Smit Apr 18 '18 at 9:42
  • $\begingroup$ You might find the answers here to be useful: mathematica.stackexchange.com/q/102132/9490 $\endgroup$ – Jason B. Apr 18 '18 at 16:15
4
$\begingroup$

If you want abrupt changes in color, Piecewise seems more appropriate.

colorWig[z_] := Piecewise[{{GrayLevel[1 - z], 0 < z < 1},
                           {Hue[.3, 1, 1 + z], -1 < z < 0}}]

DensityPlot[Sin[x y], {x, -1, 1}, {y, -1, 1}, 
 ColorFunction -> colorWig, ColorFunctionScaling -> False, 
 PlotPoints -> 50]

plot

$\endgroup$
  • $\begingroup$ This seems to be it! Thank you! $\endgroup$ – Turbotanten Apr 18 '18 at 9:53
  • $\begingroup$ @Turbotanten You're welcome. Some trial and error might be necessary to remap my sample function to the range of values your function takes. I'd suggest waiting for a while until accepting this answer to see if someone comes up with something better. Many engaged users that have more time on their hands than I do currently create truly stunning works here. $\endgroup$ – LLlAMnYP Apr 18 '18 at 9:59
4
$\begingroup$
 colorWig[z_] := 
     Which[-1 < z <= 0, ColorData["DeepSeaColors"][Rescale[z, {-1, 0}]], 
      0 <= z < 1, ColorData["AvocadoColors"][Rescale[z, {0, 1}]]]
    DensityPlot[Sin[x y], {x, -1, 1}, {y, -1, 1}, 
     ColorFunction -> colorWig, ColorFunctionScaling -> False, 
     PlotPoints -> 50, PlotLegends -> Automatic]

enter image description here

Reverse AvocadoColors

colorWig[z_] := 
 Which[-1 < z <= 0, ColorData["DeepSeaColors"][Rescale[z, {-1, 0}]], 
  0 <= z < 1, 
  ColorData[{"AvocadoColors", "Reverse"}][Rescale[z, {0, 1}]]]

enter image description here

NMaximize[{Wigner[x, y], -6 <= x <= 6, -2 <= y <= 2}, {x, y}, 
 Method -> "DifferentialEvolution"]

{0.63662, {x -> 0., y -> 0.}}

NMinimize[{Wigner[x, y], -6 <= x <= 6, -2 <= y <= 2}, {x, y}, 
 Method -> "DifferentialEvolution"]

{-0.590076, {x -> 8.73424*10^-32, y -> 0.193331}}

So we are safe to choose range [-0.6,0.64]

  colorWig[z_] := 
 Which[-0.6 < z <= 0, 
  ColorData[{"DeepSeaColors", "Reverse"}][Rescale[z, {-0.6, 0}]], 
  0 <= z < 0.65, ColorData["AvocadoColors"][Rescale[z, {0, 0.65}]]]
DensityPlot[Wigner[x, y], {x, -6, 6}, {y, -2, 2}, PlotRange -> All, 
 ColorFunction -> colorWig, ColorFunctionScaling -> False, 
 PlotLegends -> 
  Placed[BarLegend[Automatic, LegendMargins -> {{26, 20}, {-15, 0}}, 
    LegendMarkerSize -> {475, 30}], Above], 
 ImagePadding -> {{45, 20}, {45, 10}}, PlotRangePadding -> None, 
 ImageSize -> {600, 200}, AspectRatio -> Automatic, 
 FrameLabel -> {"x", "y"}, FrameStyle -> Black, 
 FrameTicksStyle -> Directive[Black, 14], 
 LabelStyle -> {Black, Bold, 14}, PlotPoints -> 50]

enter image description here

$\endgroup$
4
$\begingroup$

A useful trick for using color functions to distinguish signs is to preprocess with LogisticSigmoid[], which maps $(-\infty,\infty)$ to $(0,1)$. Applied to the OP's example:

DensityPlot[Wigner[x, y], {x, -6, 6}, {y, -2, 2}, 
            AspectRatio -> Automatic, 
            ColorFunction -> (ColorData["LightTemperatureMap", LogisticSigmoid[20 #]] &),
            ColorFunctionScaling -> False, FrameLabel -> {"x", "y"}, 
            FrameTicks -> {{{-2, 0, 2}, None}, {Table[-6 + 2 i, {i, 0, 6}], None}},
            FrameStyle -> Black, FrameTicksStyle -> Directive[Black, 14], 
            ImagePadding -> {{45, 20}, {45, 10}}, ImageSize -> {600, 200}, 
            LabelStyle -> {Black, Bold, 14}, 
            PlotLegends -> Placed[BarLegend[Automatic,
                                            LegendMargins -> {{26, 20}, {-15, 0}},
                                            LegendMarkerSize -> {475, 30}], Above],
            PlotPoints -> 75, PlotRange -> All, PlotRangePadding -> None]

Wigner function colored by sign

Personally, I prefer using "ThermometerColors":

different coloring

$\endgroup$
  • $\begingroup$ Wow this looks really good! $\endgroup$ – Turbotanten Sep 26 '18 at 7:31

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.