I have a set of data that looks like {{x1, y1, z1}, {x2, y2, z2}, ...}
so it describes points in 3D space. I want to make a heatmap out of this data. So that points with a high density are shown as a cloud and marked with different colors dependend of the density.
In fact, I want the result of this script just for 3D:
data = RandomReal[1, {100, 2}];
SmoothDensityHistogram[data, 0.02, "PDF", ColorFunction -> "Rainbow", Mesh -> 0]
Answer
If you want to plot a distribution that is three dimensional then first you need to form it! SmoothDensityHistogram
plots a smooth kernel histogram of the values $\{x_i,y_i\}$ but as we have three dimensional data here we need the function called SmoothKernelDistribution
!
data = RandomReal[1, {1000, 3}];
dist = SmoothKernelDistribution[data];
Now you have got the probability distribution with three variables. So we can simply plot the PDF
as a 3d contour plot using ContourPlot3D
. Keep in mind that this function is reputed to be little slow.
ContourPlot3D[Evaluate@PDF[dist, {x, y, z}], {x, -2, 2}, {y, -2, 2}, {z, -2, 2},
PlotRange -> All, Mesh -> None, MaxRecursion -> 0, PlotPoints -> 160,
ContourStyle -> Opacity[0.45], Mesh -> None,
ColorFunction -> Function[{x, y, z, f}, ColorData["Rainbow"][z]],
AxesLabel -> {x, y, z}]
To cut through the contours I used the option!
RegionFunction -> Function[{x, y, z}, x < z || z > y]
In order to check that the data points density is responsible for the shape of the contours we can use Graphics3D
pic = Graphics3D[{ColorData["DarkRainbow"][#[[3]]],
PointSize -> Large, Point[#]} & /@ data, Boxed -> False];
Show[con, pic]
BR
EDIT
To follow up on the 2D example and get warm colours for higher densities
data = RandomReal[1, {500, 3}];
dist = SmoothKernelDistribution[data];
ContourPlot3D[Evaluate@PDF[dist, {x, y, z}], {x, -2, 2}, {y, -2, 2},
{z, -2, 2},PlotRange -> All, Mesh -> None, MaxRecursion -> 0, PlotPoints -> 150,
ContourStyle -> Opacity[0.45], Contours -> 5, Mesh -> None,
ColorFunction -> Function[{x, y, z, f}, ColorData["Rainbow"][f/Max[data]]],
AxesLabel -> {x, y, z},
RegionFunction -> Function[{x, y, z}, x < z || z > y]]
Comments
Post a Comment