2
$\begingroup$

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?

$\endgroup$
  • 1
    $\begingroup$ Can you share a dataset to play with? $\endgroup$ – MarcoB Mar 4 at 20:48
  • $\begingroup$ Sure, two seconds $\endgroup$ – MKF Mar 4 at 20:49
  • $\begingroup$ How about the $\kappa=4$ case from mathematica.stackexchange.com/questions/13038/…, where there is an overdensity in the top left, or even the $\kappa=8,16$ case. Thanks $\endgroup$ – MKF Mar 4 at 20:54
  • $\begingroup$ 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? $\endgroup$ – MarcoB Mar 4 at 21:04
  • $\begingroup$ $f$ would probably be the number of nearest neighbours in this example $\endgroup$ – MKF Mar 4 at 21:22
6
$\begingroup$

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

$\endgroup$
  • $\begingroup$ 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! $\endgroup$ – MKF Mar 4 at 21:30
  • 1
    $\begingroup$ @MKF, maybe we can use some non-linear scaling like Sqrt instead of Rescale[...]? $\endgroup$ – kglr Mar 4 at 21:40
  • $\begingroup$ Let's check it out :) $\endgroup$ – MKF Mar 4 at 21:45
  • 1
    $\begingroup$ @MKF, please see the updated version. $\endgroup$ – kglr Mar 4 at 22:35
  • $\begingroup$ Looking great - it kind of matches the effect here using contours blendswap.com/blends/view/89381 which seems to have this sort of effect you plotted above $\endgroup$ – MKF Mar 4 at 22:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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