# How to plot a function from the sphere to the reals as a colored sphere?

Given a point $\{x,y,z\}$ on the sphere, the function

dist[x_, y_, z_] = Abs[x + y];


is some positive number. If the range of dist over the sphere happens to be e.g. $[2,5]$, then the sphere should be red when dist[x, y, z] == 2 and green when dist[x, y, z] == 5.

ParametricPlot3D[{Cos[u] Cos[v], Sin[u] Cos[v], Sin[v]}, {u, 0, 2 π}, {v, -π/2, π/2},
ColorFunction -> (Blend[{Red, Green}, dist[#, #2, #3]] &)]


doesn't work because I don't know the range of dist in advance.

I can't find a suitable plot function to write

OtherPlotFunction[dist[x, y, z], {x, y, z} ∈ Sphere[],
ColorFunction -> (Blend[{Red, Green}, #] &)]


What plot function am I missing?

• I think that something is wrong in the statement. You want to plot a colored unit sphere, however, $(x,y)$ coordinates can range to values within 2<=Abs[x+y]<=5, how can this be posible? Maybe, I am missing something... – José Antonio Díaz Navas Jan 5 '18 at 13:11
• One problem is that it should be dist[x_, y_, z_] := Abs[x + y] instead of dist[x_, y_, z_] = Abs[x + y]. Then you can just do SliceDensityPlot3D[dist[x, y, z], x^2 + y^2 + z^2 == 1, {x, -1, 1}, {y, -1, 1}, {z, -1, 1}, ColorFunction -> (Blend[{Red, Green}, #] &)]. – user484 Jan 5 '18 at 23:16
• @Rahul Very nice thank you – MeMyselfI Jan 6 '18 at 9:00

## 3 Answers

if you do not know the range a priori you can run the plot once to get it, then again to plot:

dist[x_, y_, z_] = Abs[x + y];
range = dist @@@
Reap[ParametricPlot3D[{Cos[u] Cos[v], Sin[u] Cos[v], Sin[v]}, {u,
0, 2 \[Pi]}, {v, -\[Pi]/2, \[Pi]/2},
ColorFunction :> (Sow[{#1, #2, #3}] &)]][[2, 1]] // MinMax


{0.292893, 1.70711}

ParametricPlot3D[{Cos[u] Cos[v], Sin[u] Cos[v], Sin[v]}, {u, 0,
2 \[Pi]}, {v, -\[Pi]/2, \[Pi]/2},
ColorFunction :> (Blend[{{range[], Red}, {range[], Green}},
dist[#1, #2, #3]] &)] Note that the arguments passed to the color function are scaled coordinates ranging from zero to one over the graphics box. Most likely you want to set ColorFunctionScaling -> False (both ParametricPlot3D uses), then you get the expected range,

{3.56064*10^-14, 1.41421} (* 0 to Sqrt *)

and plot: For unit sphere: $0\le |x+y|\le\sqrt{2}$:

ParametricPlot3D[{Cos[u] Sin[v], Sin[u] Sin[v], Cos[v]}, {u, 0,
2 Pi}, {v, 0, Pi}, ColorFunctionScaling -> False,
ColorFunction ->
Function[{x, y, z, u, v}, Blend[{Red, Green}, Abs[x + y]/Sqrt]],
PlotLegends -> BarLegend[{Blend[{Red, Green}, #] &, {0, Sqrt}}],
MeshFunctions -> {Abs[#1 + #2] &}, Mesh -> 5, PlotPoints -> 25] When radius == 4, 0 < Abs[x + y] < 2 Sqrt@2.

 ParametricPlot[{u, 4 Abs[Cos[u] Cos[v] + Sin[u] Cos[v]]}, {u, 0, 2 \[Pi]}, {v, -\[Pi]/2, \[Pi]/2}] dist[x_, y_, z_]   := Abs[x + y];
normalizeDist[v_]  := (v - 2)/3;
color[v_]          := Which[v >= 0 && v <= 1, Blend[{Red, Green}, v], True, White]
normalizeColor[x_, y_, z_] := dist[x, y, z] // normalizeDist // color

ParametricPlot3D[
4 {Cos[u] Cos[v], Sin[u] Cos[v], Sin[v]}, {u, 0,
2 \[Pi]}, {v, -\[Pi]/2, \[Pi]/2},
ColorFunction -> (normalizeColor[#1, #2, #3] &),
ColorFunctionScaling -> False] 