0
$\begingroup$

I have a list of points ( a list with sub lists which are {x,y} coordinates. I would like to have a listplot where the areas with higher density of points are in darker colors and areas with lower density of points have lighter colors so it is visually visible that in some areas points might overlap each other.

$\endgroup$

1 Answer 1

1
$\begingroup$
points = Join[
   RandomVariate[
    MultinormalDistribution[{-0.5, 0.25}, 0.07*IdentityMatrix[2]], 
    1000],
   RandomVariate[
    MultinormalDistribution[{0.6, -0.1}, 0.03*IdentityMatrix[2]], 
    1000],
   RandomReal[{-1.5, 1.5}, {1000, 2}]
   ];
skd = SmoothKernelDistribution[points];
ListPlot[points, 
 ColorFunction -> Function[{x, y}, Hue[PDF[skd, {x, y}]]], 
 ColorFunctionScaling -> False, AspectRatio -> 1]

points density hue

$\endgroup$
5
  • $\begingroup$ You could also use GrayLevel[1 - PDF[skd, {x, y}]] for a monochromatic gradient. Or you could use Darker@Hue[...] instead. $\endgroup$
    – flinty
    Commented Nov 10, 2020 at 15:22
  • $\begingroup$ Thank you for your help. I was wondering how did you decided to use SmoothKernelDistribution ? $\endgroup$
    – Pjana
    Commented Nov 10, 2020 at 16:47
  • $\begingroup$ Pjana the PDF of a SmoothKernelDistribution is the estimated probability density of the points. When the points are more densely compacted one would expect a higher probability density, and when they are more sparse, a lower probability density. You could visualize it with DensityPlot[PDF[skd, {x, y}],{x,-1.5,1.5},{y,-1.5,1.5}] $\endgroup$
    – flinty
    Commented Nov 10, 2020 at 17:03
  • $\begingroup$ thank you for the clarification $\endgroup$
    – Pjana
    Commented Nov 10, 2020 at 22:45
  • $\begingroup$ Do you know how can I add a legend to this type of listplot? $\endgroup$
    – Pjana
    Commented Nov 11, 2020 at 18:10

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.