I have a list of 4D data (x position, y position, amplitude, wavelength). I want to plot x, y, and amplitude on a 3D plot and have the color of the points correspond to the wavelength. I have seen many examples using functions to define color but my wavelength cannot be expressed by an analytic function. Is there a simple way to do this?
-
3$\begingroup$ Could you post what you have tried and maybe a sample of your data? $\endgroup$– s0rceCommented Feb 22, 2013 at 18:20
-
$\begingroup$ Probably can find your answer here: mathematica.stackexchange.com/questions/6521/… $\endgroup$– s0rceCommented Feb 22, 2013 at 18:24
-
1$\begingroup$ another post might be helpful:mathematica.stackexchange.com/questions/19575/… $\endgroup$– halmirCommented Feb 22, 2013 at 18:41
2 Answers
Here a another possible way to visualize 4D data:
data = Flatten[Table[{x, y, x^2 + y^2, Sin[x - y]},
{x, -Pi, Pi,Pi/10}, {y,-Pi,Pi, Pi/10}], 1];
You can use the function Point along with VertexColors. Now the points are places using the first three elements and the color is determined by the fourth. In this case I used Hue, but you can use whatever you prefer.
Graphics3D[
Point[data[[All, 1 ;; 3]], VertexColors -> Hue /@ data[[All, 4]]],
Axes -> True, BoxRatios -> {1, 1, 1/GoldenRatio}]
-
$\begingroup$ This is a good solution. I only need to figure out how to normalize the Hue (my wavelength data goes from 530 to 640) Thanks for the quick help. $\endgroup$– J-PCommented Feb 22, 2013 at 20:04
-
$\begingroup$ You should take a look at the useful function Rescale: Graphics3D[ Point[data[[All, 1 ;; 3]], VertexColors -> Hue[Rescale[#,{530,640}]] /@ data[[All, 4]]], Axes -> True, BoxRatios -> {1, 1, 1/GoldenRatio}] $\endgroup$– chuyCommented Feb 22, 2013 at 22:39
-
$\begingroup$ IS there a way to plot the function directly, without generating the points? $\endgroup$ Commented May 23, 2014 at 5:15
-
$\begingroup$ This is great. Anyway to automatically add a legend that gives some idea to what values the colors correspond? $\endgroup$– KvotheCommented May 4, 2020 at 15:29
-
1$\begingroup$ @Kvothe I'm not sure if you'd count this as automatic or not, but
Legended[Graphics3D[ Point[data[[All, 1 ;; 3]], VertexColors -> Hue /@ data[[All, 4]]], Axes -> True, BoxRatios -> {1, 1, 1/GoldenRatio}], BarLegend[{Hue, MinMax@data[[All, 4]]}]]
$\endgroup$– chuyCommented May 4, 2020 at 16:07
You can try the following approach. Let's first generate some 4D data:
ndata = 100;
data = Transpose[{RandomReal[{-1, 1}, ndata],
RandomReal[{-1, 1}, ndata], RandomReal[{0, 10}, ndata],
RandomReal[{400, 800}, ndata]}];
Now we use the first three columns as $x$, $y$ and $z$ coordinates, whereas the 4th column specifies the color of each point. The color range is specified via the Blend
function that specifies that a value of 400 should correspond to Darker[Green]
, a value of 600 to Yellow
and a value of 800 to Red
. All other values are blended colors.
ListPointPlot3D[List /@ data[[All, {1, 2, 3}]],
PlotStyle -> ({PointSize[Large],
Blend[{{400, Darker[Green]}, {600, Yellow}, {800,
Red}}, #1]} & /@ Flatten[data[[All, {4}]]])]
-
$\begingroup$ This also works. Thanks for the help. Too bad I can't select 2 answers as solution. $\endgroup$– J-PCommented Feb 22, 2013 at 20:06