13
$\begingroup$

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?

$\endgroup$
3

2 Answers 2

11
$\begingroup$

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

enter image description here

$\endgroup$
6
  • $\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-P
    Commented 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$
    – chuy
    Commented 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$
    – Kvothe
    Commented 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$
    – chuy
    Commented May 4, 2020 at 16:07
8
$\begingroup$

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

enter image description here

$\endgroup$
1
  • $\begingroup$ This also works. Thanks for the help. Too bad I can't select 2 answers as solution. $\endgroup$
    – J-P
    Commented Feb 22, 2013 at 20:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.