11
$\begingroup$

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

$\endgroup$

2 Answers 2

19
$\begingroup$

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)/
    2];
SphericalPlot3D[
 Re[SphericalHarmonicY[2, 1, Θ, Φ]], {Θ, 
  0, π}, {Φ, 0, 2 π}, 
 ColorFunction -> 
  Function[{x, y, z, Θ, Φ, r}, color[Θ, Φ]], 
 ColorFunctionScaling -> False]

Mathematica graphics

$\endgroup$
9
  • 2
    $\begingroup$ hope you don't mind the actual implementation(?) $\endgroup$
    – chris
    Dec 18, 2012 at 13:25
  • $\begingroup$ 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? $\endgroup$
    – matheorem
    Dec 18, 2012 at 13:42
  • $\begingroup$ @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. $\endgroup$
    – gpap
    Dec 18, 2012 at 14:03
  • $\begingroup$ @user15964 no, the points are calculated first, then the info is passed to the color function, so they're only calculated once. $\endgroup$
    – rcollyer
    Dec 18, 2012 at 14:16
  • $\begingroup$ @rcollyer Usually yes, but in this case the function color calculates the values again (twice). $\endgroup$
    – ssch
    Dec 18, 2012 at 14:44
15
$\begingroup$

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

$\endgroup$
6
  • $\begingroup$ Really nice, +1. $\endgroup$
    – gpap
    Dec 18, 2012 at 14:03
  • $\begingroup$ @gpap Thanks, upvoted your answer too. $\endgroup$
    – Artes
    Dec 18, 2012 at 14:05
  • $\begingroup$ @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 ? $\endgroup$
    – matheorem
    Dec 19, 2012 at 12:59
  • $\begingroup$ @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, θ, φ]`. $\endgroup$
    – Artes
    Dec 19, 2012 at 13:20
  • $\begingroup$ @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, θ, φ]] $\endgroup$
    – Artes
    Dec 19, 2012 at 13:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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