# SphericalPlot the first two column as theta phi, and the third one as color from a n*3 matrix

I'm very new to Mathematica. I'm trying to spherical plot a sphere from an n-row, 3-column matrix. The radius is a constant, the first and second columns are theta and phi of the points, and the last column is the (intensity) color corresponding to that theta, phi.

I have tried to interpolate {theta, phi} with intensity. My data has almost 20000 points(rows), so I just used 3 points to try. Following is my coding:

theta = {Pi/4, Pi/6, Pi/2}
phi = {Pi/6, 0, Pi/2}
intensity = {2, 1, 3}
dat = Table[{{theta[[m]], phi[[m]]}, intensity[[m]]}, {m, 1, Length[theta]}]
f = Interpolation[dat]
f[theta, phi]
points = N@ CoordinateTransformData["Spherical" -> "Cartesian", "Mapping", #] & /@ {{2, Pi/6, 0}, {2, Pi/4, Pi/6}, {2, Pi/2, Pi/2}};
Show[SphericalPlot3D[2, {theta, 0, Pi/2}, {phi, 0, Pi/2}, ColorFunction ->
Function[{theta, phi}, ColorData["Rainbow"][f[theta, phi]]]],
ListPointPlot3D[points, PlotStyle -> Directive[Green, PointSize -> .03]]]


And I got the result like this It shows my points lie outside the range of data, and the color change of the sphere seems irreverent from the original data points (the green ones I marked on the sphere).

I appreciate it a lot if you can help me solve this problem! Thanks!

• Try using Interpolation on your entire 2D array. Jan 16, 2022 at 17:41
• Welcome to Mathematica.SE! I hope you will become a regular contributor. To get started, 1) take the introductory tour now, 2) when you see good questions and answers, vote them up by clicking the gray triangles, because the credibility of the system is based on the reputation gained by users sharing their knowledge, 3) remember to accept the answer, if any, that solves your problem, by clicking the checkmark sign, and 4) give help too, by answering questions in your areas of expertise. Jan 16, 2022 at 17:42

First,with only 3 points you can only use "InterpolationOrder->1". Toward this aim I create new "intensity" data and function "f" like (note the point at the north pole is double, what will not harm):

dat = Flatten[Table[{{th, ph}, th ph}, {th, 0, Pi/2}, {ph, 0, Pi/2}],
1];
f = Interpolation[dat, InterpolationOrder -> 1];


To get the cartesian coordinates of the points we define

trans[{th_, ph_}] =
CoordinateTransformData["Spherical" -> "Cartesian",
"Mapping", {2, th, ph}];
points = trans /@ dat[[All, 1]] // N;


Finally we can plot:

Show[SphericalPlot3D[2, {th, 0, Pi/2}, {ph, 0, Pi/2},
ColorFunction ->
Function[{x, y, z, \[Theta], \[Phi], r},
ColorData["Rainbow"][f[\[Theta], \[Phi]]]]],
ListPointPlot3D[points,
PlotStyle -> Directive[Black, PointSize -> .03]]] • Hi Daniel, thanks for your reply. Maybe I didn't describe my question clear. What I have is a matrix with first two columns about coordinations (theta phi), and the last column is the intensity corresponding to these coordinates. So the color (intensity) is not like what you created maximum at one side of the sphere and minimum at another side. I tried to add interpolation order as 1 in my previous codes but it still give me the same result in the question. I guess probably I shouldnt use interpolation, but I dont know what else I can use...
– tll
Jan 16, 2022 at 23:55
• If you want to use "Interpolation" you must give points over the whole domain to be plotted. That is from 0 to Pi/2 for theta and phi. As I did not know the intensities there, I used arbitrary intensities. You are free to insert the right ones. Jan 17, 2022 at 8:47
• I used my whole data (almost 20k points) interpolation and plotting, but the sphere is quite strange losing lots of intensity information, (I was hoping to see a few hot spots on the sphere, instead, there is only a block of red and another block of purple on the sphere. Seems like interpolation cannot allow me to assign the color to the point on the sphere precisely.
– tll
Jan 17, 2022 at 17:05
• Try to determine what the max and min of you intensities are. And also if the intensities are evenly distributed between these 2 values or not. Jan 17, 2022 at 17:25
• The intensities range from 1577 to -2094, and they are not evenly distributed (large difference among data near two extremes and tiny difference among data near 0, I guess thats the reason I cannot precisely assign the intensity...
– tll
Jan 17, 2022 at 20:19