1
$\begingroup$

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?

$\endgroup$
  • $\begingroup$ 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... $\endgroup$ – José Antonio Díaz Navas Jan 5 '18 at 13:11
  • 1
    $\begingroup$ 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}, #] &)]. $\endgroup$ – Rahul Jan 5 '18 at 23:16
  • $\begingroup$ @Rahul Very nice thank you $\endgroup$ – MeMyselfI Jan 6 '18 at 9:00
1
$\begingroup$

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[[1]], Red}, {range[[2]], Green}}, 
     dist[#1, #2, #3]] &)]

enter image description here

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[2] *)

and plot:

enter image description here

$\endgroup$
1
$\begingroup$

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[2]]], 
 PlotLegends -> BarLegend[{Blend[{Red, Green}, #] &, {0, Sqrt[2]}}], 
 MeshFunctions -> {Abs[#1 + #2] &}, Mesh -> 5, PlotPoints -> 25]

enter image description here

$\endgroup$
0
$\begingroup$

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}]

enter image description here

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]

enter image description here

$\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.