# Plotting the local density of points: (x,y) coordinates

I'm running Mathematica 9 -- quite an old version.

I have a list of (x,y) coordinates. I wish to plot them as either a density plot or a contour plot to show the local density of points. How do I do this?

In the following representative example, the (x,y) coordinates are stored in the list data. But when I use ListDensityPlot to plot data, the result is simply a collection of horizontal lines, even when I set DataRange manually. Clearly, I don't know what I am doing.

My code and its output follows:

SeedRandom[1234];(* Make random numbers repeatable *)
(* 'uniformRandomData' consists of pseudorandom {x,y} points, with 0<=x<=8 and 0<=y<=8 *)
uniformRandomData = Table[{RandomReal[{0, 8}], RandomReal[{0, 8}]}, {10000}];

(* 'extraData'  consists of pseudorandom {x,y} points confined to a disk of
radius Sqrt[(x^2)+(y^2)] centered at {4,4} *)
thetaList = Table[RandomReal[{0, 2 Pi}], {2000}];
rList = Table[RandomReal[{0, 2}], {2000}];
extraData = Transpose[{rList*Sin[thetaList] + 4, rList*Cos[thetaList] + 4}];
(* 'data' is the union of 'uniformRandomData' and 'extraData' *)

(* ('imageSize' is the width of the plot) *)
imageSize = 350;
Grid[{{
(* ListPlot the data *)
ListPlot[data, PlotRange -> All, Frame -> True,
FrameLabel -> {"x", "y"}, PlotStyle -> Red, AspectRatio -> 1,
ImageSize -> imageSize],

(* ListDensityPlot the data *)
ListDensityPlot[data, PlotRange -> All,
DataRange -> {{0, 8}, {0, 8}}, Frame -> True,
FrameLabel -> {"x", "y"}, AspectRatio -> 1,
ImageSize -> imageSize],

(* ListDensityPlot the data (with DataRange set manually) *)
ListDensityPlot[data, PlotRange -> All,
DataRange -> {{0, 8}, {0, 8}}, Frame -> True,
FrameLabel -> {"x", "y"}, AspectRatio -> 1, ImageSize -> imageSize]
}}]


• ListDensityPlot assumes you have a grid of values, or a list of {x, y, f} triples, and makes a density plot of the values at the specified xy locations. See the documentation for ListDensityPlot.
Commented Jun 12, 2023 at 23:27

I think you are indeed misunderstanding ListDensityPlot and are instead looking for SmoothDensityHistogram.

SmoothDensityHistogram[data]


If you are looking for a visual summary of the data, then @Domen's answer is what you want. But if your data is from a random sample from some bivariate distribution and you want to make inferences about the bivariate distribution, then the following should be considered:

skd = SmoothKernelDistribution[data, Automatic, {"Bounded", {{0, 8}, {0, 8}}, "Gaussian"}];
ContourPlot[PDF[skd, {x, y}], {x, -1, 9}, {y, -1, 9}, PlotRange -> All, Exclusions -> None]


Your actual data might not be bounded so {"Bounded", {{0, 8}, {0, 8}}, "Gaussian"} would be replaced by "Gaussian".

• I think the end result is not different (other than plotting contours instead of a heat-map) than using SmoothDensityHistogram, which generates output, based on a smooth kernel density estimate. I think Gaussian is the default, but one can also manually specify the kernel SmoothDensityHistogram[data, {Automatic, "Gaussian"}]. Commented Jun 12, 2023 at 18:29
• @Domen No disagreement about the underlying use of a kernel density estimator. My issue is about the OP's objective and how their real data is generated. SmoothDensityHistogram just produces a display which is a summary of the data where SmoothKernelDensity allows probabilistic inference to be make (assuming that certain assumptions are made explicit and would likely be true). The OP doesn't mention how the real data was collected or if just a display is all that is desired.
– JimB
Commented Jun 12, 2023 at 20:27