r[\[Theta]_, \[Phi]_] := -1 ((Cos[\[Phi]] Sin[\[Theta]] Sin[\[Phi]] \
Sin[\[Theta]])^2 + (Sin[\[Phi]] Sin[\[Theta]] Cos[\[Theta]])^2 + \
(Cos[\[Phi]] Sin[\[Theta]] Cos[\[Theta]])^2);
{plot, points} =
Reap[SphericalPlot3D[
r[\[Theta], \[Phi]], {\[Theta], 0, \[Pi]}, {\[Phi], 0, 2 \[Pi]},
ColorFunction -> (ColorData["Rainbow"][Rescale[#6, {0, 1}]] &),
AxesStyle -> Thick, LabelStyle -> Directive[Black, Bold, Medium],
ImageSize -> 400, EvaluationMonitor :> Sow[r[\[Theta], \[Phi]] ]]];
Row[{plot,
DensityPlot[y, {x, -1, 1}, {y, Min@points, Max@points},
ColorFunction -> "Rainbow",
FrameTicks -> {{None, All}, {None, None}},
PlotRangePadding -> None, AspectRatio -> 15,
ImageSize -> {Automatic, 300}]}]
Why my result doesn't match my scale bar. The red part should have value -1/3 but it appears to be red.
r[\[Theta]_, \[Phi]_] := -0.8 (Sin[\[Theta]]^2) + (Sin[\[Theta]]^4);
{plot, points} =
Reap[SphericalPlot3D[
r[\[Theta], \[Phi]], {\[Theta], 0, \[Pi]}, {\[Phi], 0, 2 \[Pi]},
ColorFunction -> (ColorData["Rainbow"][Rescale[#6, {0, 1}]] &),
AxesStyle -> Thick, LabelStyle -> Directive[Black, Bold, Medium],
ImageSize -> 400,
EvaluationMonitor :> Sow[r[\[Theta], \[Phi]]]]];
Row[{plot,
DensityPlot[y, {x, -1, 1}, {y, Min@points, Max@points},
ColorFunction -> "Rainbow",
FrameTicks -> {{None, All}, {None, None}},
PlotRangePadding -> None, AspectRatio -> 15,
ImageSize -> {Automatic, 300}]}]
And for this plot, r range from {-0.15,0.15}, but it seems SphericalPlot3D
only treat abs value of r, how can I plot with {-0.15, 0.15} falls into "rainbow" range?
n = 10
abc = RandomPoint[Sphere[{0, 0, 0}, 1], n]
r[\[Theta]_, \[Phi]_] :=
Mean[Array[
1 (1 - (abc[[#1, 1]] Cos[\[Phi]] Sin[\[Theta]] +
abc[[#1, 2]] Sin[\[Phi]] Sin[\[Theta]] +
abc[[#1, 3]] Cos[\[Theta]])^2) &, n]];
minr = MinValue[r[\[Theta], \[Phi]], {\[Theta], \[Phi]}];
maxr = MaxValue[r[\[Theta], \[Phi]], {\[Theta], \[Phi]}];
{plot, points} =
Reap[SphericalPlot3D[
r[\[Theta], \[Phi]], {\[Theta], 0, \[Pi]}, {\[Phi], 0, 2 \[Pi]},
ColorFunction -> (ColorData[
"Rainbow"][(r[#4, #5] + Abs@minr)/(maxr - minr)] &),
ColorFunctionScaling -> False, AxesStyle -> Thick,
LabelStyle -> Directive[Black, Bold, Medium], ImageSize -> 400,
EvaluationMonitor :> Sow[r[\[Theta], \[Phi]] ]]];
Row[{plot,
DensityPlot[y, {x, -1, 1}, {y, Min@points, Max@points},
ColorFunction -> "Rainbow",
FrameTicks -> {{None, All}, {None, None}},
PlotRangePadding -> None, AspectRatio -> 15,
ImageSize -> {Automatic, 300}]}]
This doesn't work with @Domen 's solution, can anyone tell me why?
Rescale
and it will rescale automatically:ColorFunction -> (ColorData["Rainbow"][#6] &)
? To remove the negative values, just wrap your function in the absolute value:r[\[Theta]_, \[Phi]_] := Abs[-0.8 (Sin[\[Theta]]^2) + (Sin[\[Theta]]^4)];
$\endgroup$min = MinValue[r[\[Theta], 0], \[Theta]]; max = MaxValue[r[\[Theta], 0], \[Theta]]; r[\[Theta]_, \[Phi]_] := (-0.8 (Sin[\[Theta]]^2) + (Sin[\[Theta]]^4) + Abs@min)/(max - min);
$\endgroup$SphericalPlot
automatically rescales the range to{0, 1}
. SoColorFunction -> (ColorData["Rainbow"][#6] &)
should be what you want. Look upColorFunctionScaling
in the documentation. $\endgroup$