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.}},
MeshShading -> {Blue, Red}]

SphericalHarmonicY
, notSpericalHarmonicsY
$\endgroup$ – m_goldberg Dec 15 '19 at 16:33SphericalPlot3D
. 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 forColorFunction
are also explained for this and other plotting functions in the docs forColorFunction
, too.) $\endgroup$ – Michael E2 Dec 15 '19 at 16:44