1
$\begingroup$

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?

$\endgroup$
  • $\begingroup$ It's SphericalHarmonicY, not SpericalHarmonicsY $\endgroup$ – m_goldberg Dec 15 '19 at 16:33
  • 1
    $\begingroup$ Please edit your question to show the plot expression you are working with. $\endgroup$ – m_goldberg Dec 15 '19 at 16:38
  • $\begingroup$ 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.) $\endgroup$ – Michael E2 Dec 15 '19 at 16:44
2
$\begingroup$

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] 

enter image description here

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}]

enter image description here

$\endgroup$
  • $\begingroup$ Thank you Michael E2 and kglr. That solves my problem. Instead of the If Statement or mesh function I used: Hue[(Sign[Re[SphericalHarmonicY[l,m,[Theta],[Phi]]]]+1)/4] $\endgroup$ – Polhode Dec 15 '19 at 20:14
  • $\begingroup$ kglr - Even though your code looks correct I think your images are wrong. $\endgroup$ – Polhode Dec 15 '19 at 20:55

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