# Coloring the surface of a SphericalPlot3D using the image of a function $\Phi (r,\theta,\phi)$

So the thing is I have a SphericalPlot3D of a Potential function and I want to color it considering the image of the function.

The functions are:

fA[r_, θ_, ϕ_] = ((4/3 r^0 )*
LegendreP[0, 0, Cos[θ]]) + ((-r^1 )*
LegendreP[1, 0, Cos[θ]]) + ((1/6 r^2 )*
LegendreP[2, 0, Cos[θ]]);
fB[r_, θ_, ϕ_] = ((38/3 r^(-(0 + 1)))*
LegendreP[0, 0, Cos[θ]]) + ((-8 r^(-(1 + 1)))*
LegendreP[1, 0, Cos[θ]]) + ((16/3 r^(-(2 + 1)))*
LegendreP[2, 0, Cos[θ]]);
fδV[r_, θ_, ϕ_] = (Cos[θ] - 1)^2;

f[r_, θ_, ϕ_] =
Piecewise[{{fA[r, θ, ϕ],
r < 2}, {fB[r, θ, ϕ],
r > 2}, {fδV[r, θ, ϕ], r == 2}}]


And I'm trying to see a colored representation of f[r,θ_,ϕ_] projecting it onto the sphere.

How can I do it? I tried with

SphericalPlot3D[{fA[1, θ, ϕ], (Cos[θ - 1] - 1)^2,
fB[3, θ, ϕ]}, {θ, 0, π}, {ϕ, 0, 1.5 Pi},
PlotRange -> All,
ColorFunction ->
Function[{x, y, z, θ, ϕ, r},
ColorData["DarkRainbow"][f[r, θ, ϕ]]],
PlotPoints -> 10,
PlotLegends -> {"\!$$\*SubscriptBox[\(Φ$$, \
$$δV$$]\)(r,θ,ϕ) para r=R=2",
"\!$$\*SubscriptBox[\(Φ$$, $$B = 0$$]\)(r,θ,\
ϕ) para r=1",
"\!$$\*SubscriptBox[\(Φ$$, $$A = 0$$]\)(r,θ,\
ϕ) para r=3"}, PlotTheme -> "Detailed",
AxesLabel -> {Style["X", Bold, 16], Style["Y", Bold, 16],
Style["Z", Bold, 16]}, ViewPoint -> {2, -2, 1.5}]


for 3 values of r.

• What aspect do you want to show with the color? Commented Sep 1, 2020 at 17:29
• @CATrevillian the value of the function (f = $\Phi (r,\theta,\phi)$) with corresponds to an electric potential
– nuwe
Commented Sep 1, 2020 at 17:31
• If Phi is a function, you should include this. Using ColorFunction, you are using the right syntax, save for a wrap of Evaluate around the function being used in Function. You don’t seem to have any Phi dependence what-so-ever in your functions, though. I can get it to plot with two different colors (one for outer shell & one for inner shell) but it is not clear how it should be in the end. Can you clarify any of this? Commented Sep 1, 2020 at 17:50
• I want the color map to be the value of the function which depends on r, theta and phi: the function f composed by fA, fB, fdelta. This way I can visualize where the value of the potential is bigger on the shells
– nuwe
Commented Sep 1, 2020 at 18:40

Indeed, as CA Trevillian stated, this whole code should work, and it does. An example is

fA[r_, θ_, ϕ_] = ((4/3 r^0 )*
LegendreP[0, 0, Cos[θ]]) + ((-r^1 )*
LegendreP[1, 0, Cos[θ]]) + ((1/6 r^2 )*
LegendreP[2, 0, Cos[θ]]);
fB[r_, θ_, ϕ_] = ((8/3 r^(-(0 + 1)))*
LegendreP[0, 0, Cos[θ]]) + ((-8 r^(-(1 + 1)))*
LegendreP[1, 0, Cos[θ]]) + ((16/3 r^(-(2 + 1)))*
LegendreP[2, 0, Cos[θ]]);
fD[r_, θ_, ϕ_] = (Cos[θ] - 1)^2
f[r_, θ_, ϕ_] =
Piecewise[{{fA[r, θ, ϕ],
r < 2}, {fB[r, θ, ϕ],
r > 2}, {fD[r, θ, ϕ], r == 2}}]

SphericalPlot3D[{f[4, θ, 1], f[2, θ, 1],
f[1, θ, ϕ
]}, {θ, 0, \[Pi]}, {ϕ
, 0,
1.7 \[Pi]}, PlotRange -> All,
ColorFunction ->
Function[{x, y, z, θ, ϕ
, r},
ColorData["RedGreenSplit"][fgrande[r, θ, ϕ
]]],
ColorFunctionScaling -> True, PlotPoints -> 10,
PlotLabels -> {None, None, None}, PlotTheme -> "Detailed",
PlotLegends -> Placed["Expressions", Automatic] ,
AxesLabel -> {Style["X", Bold, 16], Style["Y", Bold, 16],
Style["Z", Bold, 16]}, ViewPoint -> {2, -1.5, 1},
ImageSize -> Large]



the resulting plot is the following:

in which, as you can see, the dependence is in the polar angle.

• Can you show that this works with an image? Your function f depends on phi, at least as written in your OP. To my understanding, this shouldn’t evaluate as f shouldn’t evaluate? Commented Sep 24, 2020 at 4:30
• @CATrevillian thanks very much for replying. The last code was wrong, I updated the solution and added a Plot to it.
– nuwe
Commented Sep 24, 2020 at 18:41
• shouldn’t your three spheres be different colors? At least, that’s what I thought. I could only ever get 2 to be different colors. Commented Sep 24, 2020 at 18:44
• Indeed, they should. My guess is that the radial response of the potential, due to the form of Legendre polynomials, doesn't play a strong role when evaluating at $r=1,2,4$. You can try, e.g., with $r= 100, 90, 40$. One of the spheres will be pale-ish while the latters will be green-ish.
– nuwe
Commented Sep 24, 2020 at 18:52
• I notice you have ColorFunctionScaling->True, is this intentional? I would think if you set it to False, there would be a change? Commented Sep 24, 2020 at 18:54