# SphericalPlot3D taking sign of $r$ into account [closed]

I want to plot Re[SphericalHarmonicY[l,m,Theta,Phi]], with the lobes having different colors depending on the sign of the function. I can’t use {x, y, z} coordinates in ColorFunction because the sign of r, (the function), cannot be determined from these.

How can I do it?

• It's SphericalHarmonicY, not SpericalHarmonicsY – m_goldberg Dec 15 '19 at 16:33
• Please edit your question to show the plot expression you are working with. – m_goldberg Dec 15 '19 at 16:38
• This is in the docs, under Options > ColorFunction, in the page for SphericalPlot3D. Dropping your function into the second example there: SphericalPlot3D[ Re[SphericalHarmonicY[2, 1, \[Theta], \[Phi]]], {\[Theta], 0, Pi}, {\[Phi], 0, 2 Pi}, ColorFunction -> Function[{x, y, z, \[Theta], \[Phi], r}, ColorData["Rainbow"][r]]] (The arguments for ColorFunction are also explained for this and other plotting functions in the docs for ColorFunction, too.) – Michael E2 Dec 15 '19 at 16:44

We can not get lobes having different colors depending on the sign of the function using the sixth argument (r) in Function[{x, y, z, θ, ϕ, r}, body] in specifying the color function because r takes only positive values.

Instead, we can use a function that depends on the first argument of SphericalPlot3D and add the option ColorFunctionScaling -> False.

For example,

SphericalPlot3D[Re[SphericalHarmonicY[3, 2, θ, ϕ]], {θ, 0, Pi}, {ϕ, 0, Pi},
BoundaryStyle -> None, Mesh -> None,
ColorFunction -> Function[{x, y, z, θ, ϕ, r},
If[Re[SphericalHarmonicY[3, 2, θ, ϕ]] <= 0, Blue, Red]],
ColorFunctionScaling -> False]


Alternatively, we can use the options MeshFunctions, Mesh and MeshShading to get the desired coloring of lobes:

SphericalPlot3D[Re[SphericalHarmonicY[3, 2, θ, ϕ]], {θ, 0, Pi}, {ϕ, 0, Pi},
BoundaryStyle -> None,
MeshFunctions -> {Function[{x, y, z, θ, ϕ, r},
Re[SphericalHarmonicY[3, 2, θ, ϕ]]]}, Mesh -> {{0.}},