7
$\begingroup$

I have 2d points and want to show a colored density plot of them. I also want to display a legend showing which point density (number of points per unit area) the colors in the plot represent.

Let's take the following 2d points:

SeedRandom[1];
points = Transpose[{RandomReal[2, 100], RandomReal[3, 100]}];
ListPlot[points, Frame -> True, PlotRange -> Full, AspectRatio -> Automatic]

enter image description here

I tried to use SmoothDensityHistogram:

SmoothDensityHistogram[points, 0.1, "PDF", ColorFunction -> "Rainbow", 
 PlotLegends -> Automatic, PlotRange -> Full, AspectRatio -> Automatic]

enter image description here

The density plot looks correct, but I would like to see a color legend labeled with the number of points and not with a probability.

What would be the Mathematike-like solution, which is e.g using for the sliding window DiskMatrix or somehow different producing the desired result.

$\endgroup$
8
$\begingroup$

The total number of points times the density gives you the number of points per area unit. What we need to do is to multiply the scale in the legend by the number of points, here is one way:

npoints = Length[points];
xlim = Sequence @@ MinMax[First /@ points];
ylim = Sequence @@ MinMax[Last /@ points];
skd = SmoothKernelDistribution[points, 0.1];

scaleMax = NMaxValue[{
    PDF[skd, {x, y}],
    Between[x, {xlim}] && Between[y, {ylim}]
    }, {x, y}, Method -> "SimulatedAnnealing"];

SmoothDensityHistogram[
 points, 0.1, "PDF",
 ColorFunction -> "Rainbow",
 PlotLegends -> BarLegend[{"Rainbow", {0, npoints scaleMax}}],
 PlotRange -> Full,
 AspectRatio -> Automatic
 ]

Density plot

Since we have already done most of the work, we might also create the plot directly using DensityPlot:

DensityPlot[
 PDF[skd, {x, y}], {x, xlim}, {y, ylim},
 ColorFunction -> "Rainbow",
 PlotLegends -> BarLegend[{"Rainbow", {0, npoints scaleMax}}],
 PlotPoints -> 50,
 AspectRatio -> Automatic
 ]

Finally, we should note that the density does not actually say how many points there are in a specific region. It will tell us only that according to the distribution, statistically, that is how many points we would typically find there.

NIntegrate[
 npoints PDF[skd, {x, y}], Evaluate[{x, xlim}], Evaluate[{y, ylim}],
 Method -> "MonteCarlo"
 ]

91.0419

This tells us that statistically the expected number of points inside the plot range, if there are 100 points overall, would be 91. Increasing the plot range a bit will include almost all of them:

NIntegrate[
 npoints PDF[skd, {x, y}], {x, -1, 3}, {y, -1, 4},
 Method -> "MonteCarlo"
 ]

99.905


Addendum

Here is a version which corresponds to the self-answer by mrz:

SmoothDensityHistogram[
 points, 0.1, "PDF",
 ColorFunction -> "Rainbow",
 PlotLegends -> BarLegend[{"Rainbow", {0, npoints Pi 0.2^2 scaleMax}}],
 PlotRange -> Full, 
 AspectRatio -> Automatic
 ]

Density plot 2

The difference is that instead of showing the expected number of points inside one area unit, we show the expected number of points inside a region of an area corresponding to a circle of radius 0.2.


Addendum 2

Here is how to use a specified range for the color bar:

cf = Function[x, ColorData["Rainbow"][x/80]];

DensityPlot[
 npoints PDF[skd, {x, y}], {x, xlim}, {y, ylim},
 ColorFunction -> cf,
 ColorFunctionScaling -> False,
 PlotLegends -> BarLegend[{cf, {0, 80}}],
 PlotPoints -> 50,
 AspectRatio -> Automatic
 ]

Density plot with fixed color bar range

In this example, the range goes from 0 to 80. The key points to take note of is that I turned off function scaling with ColorFunctionScaling -> False and provided my own custom color function for both the color bar and the density plot.

If I had used SmoothDensityHistogram instead of DensityPlot then its color function would have to account for the fact that the probability density ranges from 0 to 1 and not 0 to 80, as the number of points in this example.

$\endgroup$
  • $\begingroup$ Thanks a lot for the solution. Please excuse me, but I think I do not understand the PDF result: When I look at the red spot at about {0.5,2.4} then the legend shows that there are about 50 points. How can that be if we have only 100 points in total? $\endgroup$ – mrz Nov 19 '17 at 10:15
  • 1
    $\begingroup$ @mrz It represents the density of points at that location, just like the density of water is 1kg/m^3, you have a density of 50 points per unit area at that location. It's the only sensible answer to your question I can think of, it is the only kind of information you will get from a density plot. Be it a density of probability or something else we can never know what happens at a specific point. Points are infinitesimally small. If you have some water then you know that the density is 1kg/m^3 and the weight of the water at (x,y,z) is zero because (x,y,z) has an infinitesimal volume. $\endgroup$ – C. E. Nov 19 '17 at 11:21
  • $\begingroup$ @mrz I've added a version which corresponds to your answer. $\endgroup$ – C. E. Nov 19 '17 at 20:42
  • $\begingroup$ Many thanks for your help. $\endgroup$ – mrz Nov 20 '17 at 5:31
  • 1
    $\begingroup$ @mrz I wasn't at a computer with Mathematica, so I couldn't answer earlier. I've updated the answer now to address your question. $\endgroup$ – C. E. Nov 20 '17 at 19:19
1
$\begingroup$

This is my old solution, before I found SmoothDensityHistogram. It shows the number of points in a circular region (radius=0.2):

dx = 0.1; dy = 0.1;
x0 = 0; y0 = 0;
xmax = 2; ymax = 3;
radius = 0.2;

number = Array[0 &, {Round@(xmax/dx), Round@(ymax/dy)}];

Do[
  number[[i, j]] = Length@Nearest[points, {x0 + (i - 1)*dx + dx/2, y0 + (j - 1)*dy + dy/2}, 
  {All, radius}];
  , {i, 1, Round@(xmax/dx), 1}
  , {j, 1, Round@(ymax/dy), 1}
  ];
ListDensityPlot[Transpose@number, ColorFunction -> "Rainbow", PlotLegends -> Automatic, 
 AspectRatio -> Automatic]

enter image description here

$\endgroup$

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.