Mathematica Stack Exchange is a question and answer site for users of Mathematica. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I want to color a spherical harmonics. So I write as follows.

color[θ_, φ_] := 
  RGBColor[(Sign[Re[SphericalHarmonicY[2, 1, θ, φ]]] + 1)/2, 0, 
           (-Sign[Re[SphericalHarmonicY[2, 1, θ, φ]]] + 1)/2 ];
SphericalPlot3D[ Re[SphericalHarmonicY[2, 1, θ, φ]], { θ, 0, Pi}, {φ, 0, 2 Pi}, 
                 ColorFunction -> Function[{x, y, z, θ, φ, r}, color[ θ, φ]]]

I expect that the output plot should show the parity of the spherical harmonics with red corresponding to the positive part and blue corresponding to the negative part. But the actual result is all Blue!

spherical harmonic

share|improve this question
up vote 16 down vote accepted

You need to add ColorFunctionScaling -> False as an option to SphericalPlot3D. That should do the trick

color[Θ_, Φ_] := 
  RGBColor[(Sign[Re[SphericalHarmonicY[2, 1, Θ, Φ]]] + 1)/
    2, 0, (-Sign[Re[SphericalHarmonicY[2, 1, Θ, Φ]]] + 1)/
 Re[SphericalHarmonicY[2, 1, Θ, Φ]], {Θ, 
  0, π}, {Φ, 0, 2 π}, 
 ColorFunction -> 
  Function[{x, y, z, Θ, Φ, r}, color[Θ, Φ]], 
 ColorFunctionScaling -> False]

Mathematica graphics

share|improve this answer
hope you don't mind the actual implementation(?) – chris Dec 18 '12 at 13:25
Thank you! your solution works. I have an extra question. I somewhat think my code is not so efficient. I was wandering whether mathematica will calculate SphericalHarmonic twice, once in the plot body and once in the colorfunction body. Do you have any better way? – matheorem Dec 18 '12 at 13:42
@chris thank you so for the implementation! As per improving the code, the answer by Artes would probably get you started - it is a much more detailed/better answer than mine. – gpap Dec 18 '12 at 14:03
@user15964 no, the points are calculated first, then the info is passed to the color function, so they're only calculated once. – rcollyer Dec 18 '12 at 14:16
@rcollyer Usually yes, but in this case the function color calculates the values again (twice). – ssch Dec 18 '12 at 14:44

There are many ways of coloring functions, to visualize spatial dependence of spherical harmonics one can take advantage of a useful function Rescale, so here is a bit different coloring using also imaginary part of the function :

col[θ_, φ_] := RGBColor @ Rescale[{  Re @ #, 0, -Im @ #}]& @ SphericalHarmonicY[2, 1, θ, φ]
SphericalPlot3D[ Re[ SphericalHarmonicY[2, 1, θ, φ]], { θ, 0, Pi}, { φ, 0, 2 Pi},   
                 ColorFunction -> Function[{x, y, z, θ, φ, r}, col[ θ, φ]],
                 ColorFunctionScaling -> False ] 

enter image description here

share|improve this answer
Really nice, +1. – gpap Dec 18 '12 at 14:03
@gpap Thanks, upvoted your answer too. – Artes Dec 18 '12 at 14:05
@Artes thank you! But forgive my poor mathematica IQ. I just can't understand the col function, especially the "&@", could you explain it to me ? – matheorem Dec 19 '12 at 12:59
@user15964 f @ x means exactly f[x] i.e. function f acts on the argument x. Wherever you find # and & ( shorthands for Slot and Function respectively, see the documentation pages) that means we deal with pure functions, read tutorial/PureFunctions in the VirtualBook. So here RGBColor @ Rescale[{ Re @ #, 0, -Im @ #}]& @ SphericalHarmonicY[2, 1, θ, φ] means that RGBColor acts on Rescale which has arguments, e.g. ` Re @ #` i.e. Re acts on something, this & @ denotes that the function body ends and ˛it acts on the argument SphericalHarmonicY[2, 1, θ, φ]`. – Artes Dec 19 '12 at 13:20
@user15964 You should also check FullForm[RGBColor@Rescale[{Re@#, 0, -Im@#}] &@ SphericalHarmonicY[2, 1, Pi/3, Pi/4]] and Trace[RGBColor @ Rescale[{ Re @ #, 0, -Im @ #}]& @ SphericalHarmonicY[2, 1, θ, φ]] – Artes Dec 19 '12 at 13:27

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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