2

Suppose I have a bunch of 3d data points, is there a way to plot a ListDensityPlot3D such that the opacity is determined by the number of nearest points to each datapoint?

Meaning, in dense point regions, the view is more opaque, and in less dense regions, the transparency increases?

MarcoB
  • 67,153
  • 18
  • 91
  • 189
MKF
  • 591
  • 2
  • 10
  • 1
    Can you share a dataset to play with? – MarcoB Mar 04 '19 at 20:48
  • Sure, two seconds – MKF Mar 04 '19 at 20:49
  • How about the $\kappa=4$ case from https://mathematica.stackexchange.com/questions/13038/vectors-with-a-certain-magnitude-in-mathematica, where there is an overdensity in the top left, or even the $\kappa=8,16$ case. Thanks – MKF Mar 04 '19 at 20:54
  • I am not sure I completely understand. You need 4D points as input for ListDensityPlot3D, i.e. $(x,y,z,f)$. What should the value of $f$ be for your points? I was hoping you could share an actual data set in MMA format for us to play with. Also, if your points are denser, and they are represented by a non-transparent symbol in a 3D plot, wouldn't the effect you seek sort of happen naturally? That is, where you have more points, there plot is less transparent because of the points themselves? – MarcoB Mar 04 '19 at 21:04
  • $f$ would probably be the number of nearest neighbours in this example – MKF Mar 04 '19 at 21:22
  • "That is, where you have more points, there plot is less transparent because of the points themselves?" - Exactly; perhaps a better way of phrasing it would be to smooth the points out instead of ListDensityPlot them? – MKF Mar 04 '19 at 21:24
  • Does this question lead you to the solution? https://mathematica.stackexchange.com/q/191298/4346 – Greg Hurst Mar 04 '19 at 22:06
  • When I asked that, this differs slightly because I ask that the density be mirrored by the local density of a point. You could for example imagine identical spheres around each point, count the number of points in the sphere to estimate the local density, then use this parameter to plot an opacity in the vicinity of each point. They are related – MKF Mar 04 '19 at 22:09

1 Answers1

7

Using J.M.'s vonMisesFisherRandom:

SeedRandom[1]
table = With[{μ = {-1/Sqrt[8], -Sqrt[3/8], 1/Sqrt[2]}, κ =  4}, 
  Table[vonMisesFisherRandom[μ, κ], {10^3}]];
radius = .05;
data = DeleteDuplicatesBy[Join[table, 
  List /@ Length /@ Nearest[table, table, {All, radius}], 2], #[[;; 3]] &];
Row[{ListPointPlot3D[table, PlotStyle -> AbsolutePointSize[1], 
   ImageSize -> Medium, BoxRatios -> 1], 
  ListDensityPlot3D[data, 
   ColorFunction -> (Opacity[N@Log[# + 1], Blend[{{0, White}, {1, Red}}, #]] &),
   ImageSize -> Medium, 
   PlotLegends -> Automatic]}]

enter image description here

You can also use the option OpacityFunction as follows:

ListDensityPlot3D[data, 
 OpacityFunction -> (#/5 &), 
 ImageSize -> Medium, PlotLegends -> Automatic]

enter image description here

Add the option ColorFunction -> (Blend[{{0, White}, {1, Red}}, #]&) to get

enter image description here

kglr
  • 394,356
  • 18
  • 477
  • 896
  • Very nice, thanks! Is there a way to recover somehow the "smoothness" of the points distribution? It looks very red in that region on the left! – MKF Mar 04 '19 at 21:30
  • 1
    @MKF, maybe we can use some non-linear scaling like Sqrt instead of Rescale[...]? – kglr Mar 04 '19 at 21:40
  • Let's check it out :) – MKF Mar 04 '19 at 21:45
  • 1
    @MKF, please see the updated version. – kglr Mar 04 '19 at 22:35
  • Looking great - it kind of matches the effect here using contours https://www.blendswap.com/blends/view/89381 which seems to have this sort of effect you plotted above – MKF Mar 04 '19 at 22:38