3
$\begingroup$

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.

  • How can I use ListDensityPlot to visually show that the density of points is largest in a disk of radius 2 centered at (4,4)?

  • Or should I be using some other method such as ListContourPlot or ArrayPlot? Or even ListPlot with some sort of coloring function?

  • Am I completely not understanding what ListDensityPlot does and how it works?

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' *)
data = Join[uniformRandomData, 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]
   }}]

plot output

$\endgroup$
1
  • $\begingroup$ 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. $\endgroup$
    – tad
    Commented Jun 12, 2023 at 23:27

2 Answers 2

7
$\begingroup$

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

SmoothDensityHistogram[data]

enter image description here

$\endgroup$
6
$\begingroup$

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]

Contour plot of bivariate density

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

$\endgroup$
2
  • $\begingroup$ 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"}]. $\endgroup$
    – Domen
    Commented Jun 12, 2023 at 18:29
  • 2
    $\begingroup$ @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. $\endgroup$
    – JimB
    Commented Jun 12, 2023 at 20:27

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.