1
$\begingroup$

I am trying to understand what is happening in the following code (taken from here):

SphericalPlot3D[1, {θ, 0, π}, {Φ, 0, 2 π}, 
     ColorFunction -> Function[{x, y, z, θ, Φ, r}, 
         ColorData["DarkRainbow"][Cos[5 θ] + Cos[4 Φ]/2]], 
     ColorFunctionScaling -> False, Mesh -> False, Boxed -> False, Axes -> False]

As only θ and Φ appear in the function, I thought on removing {x, y, z} from the Function parameters. But then I get a different plot. Why?

How is the function evaluated, what is happening behind the scenes?

The docs are not clear on how the arguments are passed.

https://reference.wolfram.com/language/ref/ColorFunction.html

https://reference.wolfram.com/language/ref/SphericalPlot3D.html?q=SphericalPlot3D

https://reference.wolfram.com/language/ref/ColorData.html?q=ColorData

$\endgroup$
0
$\begingroup$

The arguments to functions correspond to the "slot" number, not the symbolic name used to represent the slot (unlike, say, Python's keyword arguments).

In

Function[{x, y, z, θ, Φ, r}, ...]

the symbol θ represent slot 4 and Φ represents slot 5.

In

Function[{θ, Φ, r}, ...]

the symbol θ represent slot 1 and Φ represents slot 2.

According to the docs for SphericalPlot3D, the slots for ColorFunction are filled as follows:

  • Slot 1 is passed the $x$ coordinate.
  • Slot 2 is passed the $y$ coordinate.
  • Slot 3 is passed the $z$ coordinate.
  • Slot 4 is passed the $\theta$ coordinate.
  • Slot 5 is passed the $\phi$ coordinate.
  • Slot 6 is passed the $r$ coordinate.

The ColorFunction in the example can also be coded using Slot[n] (#n) and the slot numbers:

ColorFunction -> (ColorData["DarkRainbow"][Cos[5 #4] + Cos[4 #5]/2] &)

but the symbolic form is easier to read, imo.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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