# ListDensityPlot3D with opacity

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?

• Can you share a dataset to play with? Commented Mar 4, 2019 at 20:48
• Sure, two seconds
– MKF
Commented Mar 4, 2019 at 20:49
• 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
– MKF
Commented Mar 4, 2019 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? Commented Mar 4, 2019 at 21:04
• $f$ would probably be the number of nearest neighbours in this example
– MKF
Commented Mar 4, 2019 at 21:22

Using J.M.'s vonMisesFisherRandom:

SeedRandom[1]
table = With[{μ = {-1/Sqrt[8], -Sqrt[3/8], 1/Sqrt[2]}, κ =  4},
Table[vonMisesFisherRandom[μ, κ], {10^3}]];
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]}]


You can also use the option OpacityFunction as follows:

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


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

• 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
Commented Mar 4, 2019 at 21:30
• @MKF, maybe we can use some non-linear scaling like Sqrt instead of Rescale[...]?
– kglr
Commented Mar 4, 2019 at 21:40
• Let's check it out :)
– MKF
Commented Mar 4, 2019 at 21:45
• @MKF, please see the updated version.
– kglr
Commented Mar 4, 2019 at 22:35
• 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
– MKF
Commented Mar 4, 2019 at 22:38