1
$\begingroup$

Is there a way to convert an array of values $(x_i,y_i,z_i), i=1,\ldots,n$ to a density plot instead of a ListPointPlot3D?

I don't know if this is duplicated, but somehow, I cannot seem to just use ListDensityPlot3D?

$\endgroup$
1
  • $\begingroup$ Notice that ListDensityPlot does not work with just points either. You can do something like this: mathematica.stackexchange.com/q/159644/5478 in 3D. Which seems to work. $\endgroup$
    – Kuba
    Commented Feb 11, 2019 at 13:44

2 Answers 2

5
$\begingroup$

Hard to know exactly what you want without the explicit data, but maybe you'd like to bin the data?

SeedRandom[1234];
pts = RandomReal[{0, 1}, {1000, 3}];

centers = Tuples[Range[.1, .9, .2], 3];

nf = Nearest[centers];

ListDensityPlot3D[KeyValueMap[Append, CountsBy[pts, First@*nf]]]

$\endgroup$
2
  • $\begingroup$ Hi Chip, something like this is the kind of thing exactly! But is it possible, rather than grey and orange, to have high density regions be more opaque and low density more transparent? $\endgroup$
    – MKF
    Commented Feb 12, 2019 at 5:33
  • $\begingroup$ See OpacityFunction in the ListDensityPlot3D ref page here. $\endgroup$
    – Greg Hurst
    Commented Feb 12, 2019 at 13:22
4
$\begingroup$

Using Chip Hurst's example data:

SeedRandom[1234];
pts = RandomReal[{0, 1}, {1000, 3}];

SmoothKernelDistribution + DensityPlot3D

pdf[x_, y_, z_] := PDF[SmoothKernelDistribution[pts, MaxExtraBandwidths -> 0, 
   MaxMixtureKernels -> All], {x, y, z}]
DensityPlot3D[Evaluate[pdf[x, y, z]], {x, 0, 1}, {y, 0, 1}, {z, 0, 1},
  ColorFunction -> (Directive[Opacity[#], Blend[{{0, White}, {0.5, Blue}, {1, Red}}, #]] &), 
 PlotLegends -> Automatic]

enter image description here

DensityPlot3D[Evaluate[pdf[x, y, z]], {x, 0, 1}, {y, 0, 1}, {z, 0, 1},
  OpacityFunction -> Function[f, f/20],
 ColorFunction -> (Blend[{{0, White}, {0.5, Blue}, {1, Red}}, #] &), 
 PlotLegends -> Automatic]

enter image description here

HistogramList + ListDensityPlot3D

ListDensityPlot3D[HistogramList[pts, 10][[2]], DataRange -> {{0, 1}, {0, 1}, {0, 1}}]

enter image description here

$\endgroup$
3
  • $\begingroup$ This is the kind of thing kglr, is there a way to change it so the density points is displayed as a function of opacity? ie the more points in a neighbourhood, the denser the opacity/region? $\endgroup$
    – MKF
    Commented Feb 12, 2019 at 8:27
  • $\begingroup$ @MKF, i updated with a ColorFunction that makes denser regions more opaque. $\endgroup$
    – kglr
    Commented Feb 12, 2019 at 8:46
  • $\begingroup$ Wow you are amazing, thank you so much for your guidance! $\endgroup$
    – MKF
    Commented Feb 12, 2019 at 9:11

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.