1

I have a table with 10 columns, obtained as import from file. I would like to use 4 columns as {x, y, z, u} params for BubbleChart3d, and an additional column to color the points. I'm unable to make this work. The code I have is like this:

t is table from a file import. I am using columns 1, 2, 9, 10 for xyz-coordinates and size of the bubble, I an trying to use column 7 for color of the bubble

BubbleChart3D[Map[Function[r, {r[[1]], r[[2]], r[[9]], r[[10]]}]][t], 
  ColorFunction -> Map[Function [c, RGBColor[1, 0, c[[7]]]]][t]]

This doesn't work — I get a bubble plot with same color. The 7th column does have differing values. What am I doing wrong?

Example of table t:

{{1, -1, 1, 0, 15, 0, 82899, 177, 1, 0}, {1, -1, 1, 0, 15, 0, 10231991, 177, 1, 0}, 
 {1, 0, 1, 0, 15, 0, 4633, 177, 1, 0}, {2, -1, 2, 0, 0, 0, 10231991, 204, 1, 1}, 
 {2, 1, 2, 0, 0, 0, 0, 204, 0, 1}, {4, 3, 4, 4, 354, 1, 2, 13, 0, 2}, 
 {4, -1, 4, 4, 354, 1, 10231991, 13, 1, 2}, {4, 0, 4, 4, 354, 1, 4633, 13, 1, 2}, 
 {4, 4, 4,4, 354, 1, 0, 13, 0, 2}, {5, 5, 5, 0, 0, 0, 0, 64, 0, 2}, 
 {5, -1, 5, 0, 0, 0, 82899, 64, 1, 2}, {5, -1, 5, 0, 0, 0, 10231991, 64, 1, 2}, 
 {5, 0, 5, 0, 0, 0, 4633, 64, 1, 2}, {5, 6, 5, 0, 0, 0, 0, 64, 0,2}, 
 {6, 7, 6, 2, 0, 0, 0, 519, 0, 3}, {6, 8, 6, 2, 0, 0, 0, 519, 0,3}, 
 {6, -1, 6, 2, 0, 0, 10231991, 519, 1, 3}, {6, 9, 6, 2, 0, 0, 0, 519, 0, 3}, 
 {6, -1, 6, 2, 0, 0, 82899, 519, 1, 3}, {7, -1, 7, 0, 0, 0, 10231991, 0, 1, 0}}
m_goldberg
  • 107,779
  • 16
  • 103
  • 257
Darshat
  • 11
  • 1

2 Answers2

1

Note: I rescaled radius and color values.

data = {{1, -1, 1, 0, 15, 0, 82899, 177, 1, 0}, {1, -1, 1, 0, 15, 0, 
        10231991, 177, 1, 0}, {1, 0, 1, 0, 15, 0, 4633, 177, 1, 
        0}, {2, -1, 2, 0, 0, 0, 10231991, 204, 1, 1}, {2, 1, 2, 0, 0, 0, 
        0, 204, 0, 1}, {4, 3, 4, 4, 354, 1, 2, 13, 0, 2}, {4, -1, 4, 4, 
        354, 1, 10231991, 13, 1, 2}, {4, 0, 4, 4, 354, 1, 4633, 13, 1, 
        2}, {4, 4, 4, 4, 354, 1, 0, 13, 0, 2}, {5, 5, 5, 0, 0, 0, 0, 64, 
        0, 2}, {5, -1, 5, 0, 0, 0, 82899, 64, 1, 2}, {5, -1, 5, 0, 0, 0, 
        10231991, 64, 1, 2}, {5, 0, 5, 0, 0, 0, 4633, 64, 1, 2}, {5, 6, 5,
         0, 0, 0, 0, 64, 0, 2}, {6, 7, 6, 2, 0, 0, 0, 519, 0, 3}, {6, 8, 
        6, 2, 0, 0, 0, 519, 0, 3}, {6, -1, 6, 2, 0, 0, 10231991, 519, 1, 
        3}, {6, 9, 6, 2, 0, 0, 0, 519, 0, 3}, {6, -1, 6, 2, 0, 0, 82899, 
        519, 1, 3}, {7, -1, 7, 0, 0, 0, 10231991, 0, 1, 0}};
    data2 = data[[All, {1, 2, 9, 10, 7}]];
    minMax = (MinMax@data2[[All, 5]])^(1/10);
    vec = {{0, 0, 165}, {40, 50, 220}, {90, 120, 240}, {140, 180, 
        247}, {195, 223, 240}, {230, 227, 230}, {240, 223, 195}, {247, 
        180, 140}, {240, 120, 90}, {220, 50, 40}, {164, 0, 0}};
    col = Blend["DarkRainbow", Rescale[#, minMax]] &;
    Graphics3D[{col[Last[#]], 
        Sphere[{#[[1]], #[[2]], #[[3]]}, (#[[4]])^(1/10)]} & /@ data2, 
     Axes -> True, ImageSize -> Large]

enter image description here

OkkesDulgerci
  • 10,716
  • 1
  • 19
  • 38
  • Thanks Okkes. This worked. What I struggled with later was to scale the z-axis differently, possibly because I'm newcomer. The actual data set has over million points so x, y are pretty large and the z-axis between 0-1 was pretty squished. Still it does the job, and I learnt that the built in bubblechart can be done manually! – Darshat May 05 '19 at 15:46
1

You can pass the desired color for each data point as metadata as follows:

BubbleChart3D[Style[#, RGBColor[1, 0, #2]] & @@@ 
  Transpose[{t[[All, {1, 2, 9, 10}]], Rescale@t[[All, 7]]}]]

enter image description here

You can replace Style with Rule above (i.e., replace Style[#, RGBColor[1, 0, #2]] & with # -> RGBColor[1, 0, #2]&) to get the same result.

Alternatively, you can apply Log transformation to column 7 (after adding 1 and before rescaling):

BubbleChart3D[# -> RGBColor[1, 0, #2] & @@@ 
  Transpose[{t[[All, {1, 2, 9, 10}]], Rescale[Log[1 + t[[All, 7]]]]}]]

enter image description here

You can also use a different color function (say, ColorData["DarkRainbow"] instead of RGBColor[1,0,#]&):

BubbleChart3D[# -> ColorData["DarkRainbow"][ #2] & @@@ 
  Transpose[{t[[All, {1, 2, 9, 10}]], Rescale[Log[1 + t[[All, 7]]]], data[[All, 7]]}]]

enter image description here

kglr
  • 394,356
  • 18
  • 477
  • 896
  • Thanks kglr. This works great, but I didn't get how its doing the trick. The data passed to the "constructor" of BubbleChart3d looks something like: ' {{2,1,0,1}-->RGBColor, {4,3,0,2}->RGBColor,… }. This is the metadata form but I cannot find any documentation on it, would have never discovered this myself! – Darshat May 05 '19 at 15:47
  • @Darshat, in BubbleChart3D >> Details and Options Style is in the list of functions that can be used to wrap the data. Rule[{xi,yi,zi,ui}, color] as a wrapper gives the same result as Style[{xi,yi,zi,ui}, color] (this behavior is undocumented afaik) . – kglr May 06 '19 at 00:34