# Color a phase and density of a density plot

I want to plot a Laguerre-Gaussian,

LG[r_, \[Phi]_, p_, l_, w_] := (
Sqrt[(2 p!)/(\[Pi] (p + Abs[l])!)]
1/w E^(-r^2/w^2)
((r Sqrt[2])/w)^Abs[l]
LaguerreL[p, Abs[l], 2 r^2/w^2]
E^(I l \[Phi])
)


such as I have done here,

DensityPlot[
Evaluate[
Abs@LG[
Sqrt[x^2 + y^2],
ArcTan[x, y],
1, 0, 1
]
]
, {x, -2, 2}
, {y, -2, 2}
, Mesh -> None
, PlotPoints -> 50
, PlotRange -> All
, ImageSize -> {400, 400}
]


but I am wanting to also show the phase in this plot, so like a full color bar to the side of $\phi$ from -$\pi$ to $\pi$, and the plot would show a different color depending on the phase.

To further clarify, the Laguerre-Gaussian has a phase associated value (it is a function of $\phi$ and currently I'm only seeing the radial value). The spiral phase is seen in the $e^{i l \phi}$ term. The figure below is a representation of what I am trying to accomplish (but obviously I just need one).

So to wrap it up, the first plot shows density, and I also want it to show phase.

• Not clear to me what you want to see at the end. Would replacing the Abs in your DensityPlot expression with Arg not do this? Or are you hoping to plot both quantities in a single plot somehow (e.g. by using the hue for phase and the saturation for amplitude)?
– nben
Commented Jul 11, 2018 at 17:37
• Indentation makes your code more readable and easier to select to copy. Commented Jul 11, 2018 at 17:50
• @rhermans Thanks for the help to make it easier to read (I haven't been on stack exchange very long). Also I updated my question to clarify and show an example.
– Josh
Commented Jul 11, 2018 at 19:45

## 1 Answer

Since the ColorFunction in DensityPlot only accepts a real-valued function value as its single argument, you have to play some tricks to combine intensity and phase information as in the example plots.

Here is one way to do it:

LG[r_, ϕ_, p_, l_, w_] :=
Sqrt[(2 p!)/(Pi (p + Abs[l])!)] 1/w E^(-r^2/w^2) ((r Sqrt[2])/w)^
Abs[l] LaguerreL[p, Abs[l], 2 r^2/w^2] E^(I l ϕ)

LGPlot[l_, p_, w_: 1, xMax_: 3, plotPoints_: 50, colors_: Hue] := Show[
DensityPlot[
Evaluate[
Arg@LG[Sqrt[x^2 + y^2], ArcTan[x, y], p, l, w]],
{x, -xMax, xMax}, {y, -xMax, xMax},
ColorFunction -> (colors[(Pi + #)/(2 Pi)] &),
ColorFunctionScaling -> False, Exclusions -> None,
PlotPoints -> plotPoints],
DensityPlot[
Evaluate[
Abs@LG[Sqrt[x^2 + y^2], ArcTan[x, y], p, l, w]], {x, -xMax,
xMax}, {y, -xMax, xMax}, Background -> None,
ColorFunction -> (RGBColor[0, 0, 0, 1 - #] &),
PlotPoints -> plotPoints, PlotRange -> All, Exclusions -> None],
Background -> Black, Frame -> None]

GraphicsGrid[
Table[Show[LGPlot[l, p], ImageSize -> 200], {p, 0, 2}, {l, -2, 2}],
Background -> Black]


The approach I chose is to make DensityPlots of Abs and Arg separately in the same range, and the superimpose them with Show. To get the desired coloring out of the superposition, the second plot on top encodes the Abs of the function purely in the alpha channel of a ColorFunction with an otherwise black color. This then lets the first plot shine through only where the intensity is nonzero. The first plot encodes the phase of the function with a different ColorFunction, using the fixed known range of Arg (from $-\pi$ to $\pi$).

I defined everything as a function LGPlot where you can specify the parameters of LG and also the size of the plot range, and the color function.

Here is an example where I changed the plot range from the default of 3 to 5:

GraphicsGrid[
Table[Show[LGPlot[l, p, 1, 5], ImageSize -> 200],
{p, 0, 2}, {l, -2, 2}], Background -> Black]


In principle you could also try to adapt one of the solutions in Compiling ColorFunction for faster complex phase-amplitude plots, but these aren't based on DensityPlot. Instead, the starting point there is a list of discrete values of the complex function which can be plotted with ArrayPlot. However, then you'll have to define a single ColorFunction in which you either use ColorFunctionScaling or not. The way I do it here, I'm able to use ColorFunctionScaling for the modulus, and not for the phase. This is important because the $\ell = 0$ results will look wrong unless ColorFunctionScaling -> False; and on the other hand with this same choice the modulus won't look good because it doesn't automatically use the available brightness range.

• Thanks this is awesome, overlapping the Arg and Abs plots. One last question that was lost in the overall details (but was stated in my original question), how can I get the color bar to the side? I tried PlotLegends -> Automatic and this places the bar plot but nothing inside. Normally this works, but for some reason it's not able to evaluate the function inside ColorFunction? Thanks!
– Josh
Commented Jul 12, 2018 at 12:43
• I answered my own question. Change ColorFunctionScaling -> None to False.
– Josh
Commented Jul 12, 2018 at 13:08
• You're right, I should have used False instead of None (was just lucky it worked anyway). I've corrected it.
– Jens
Commented Jul 12, 2018 at 14:52