Skip to main content

plotting - ListPlot coloring by density of points


I have a set of points on a circumference:


points = Map[Normalize, RandomReal[{-1, 1}, {3000, 2}]]

And I want to plot them and color the plot according to the density of points. You see, ListPlot[points] will just give me that, a simple plot, on which I can't see the distribution of the points on the circle.



Maybe there is an option?


I hope you guys can understand what I'm asking.


Edit/Follow-up


I used SmoothDensityHistogram[points, ColorFunction -> "Rainbow"]


This is what it looks like


Now, it's not really what I'm looking for. I kind of wanted it to look like the original plot, just the circumference.



Answer



Here is what I think you wanted:


points = Map[Normalize, RandomReal[{-1, 1}, {3000, 2}]];


d = SmoothKernelDistribution[points];

colors = Hue /@ Rescale[PDF[d, #] & /@ points];

Graphics[Transpose[{colors, Point /@ points}]]

pic


Here the SmoothKernelDistribution is evaluated in the plane, giving you a two-dimensional interpretation of density. One could also understand your question as asking for a one-dimensional density of points only on the circle. But I followed the simplest interpretation here.


For completeness, here is an implementation using ListPlot. The colors are contained in PlotStyle, but in order to make them apply to each point individually I have to add another level of depth to the list points:


ListPlot[Map[List, points], PlotStyle -> colors, 

AspectRatio -> Automatic]

listPlot


I may as well add the treatment of the density as purely one-dimensional on the circle: here I convert the elements of points to their polar angle coordinate, and then calculate the density in this polar angle:


arc = ArcTan /@ points;

d = SmoothKernelDistribution[arc];

colors = Hue /@ Rescale[PDF[d, #] & /@ arc];


Then proceed with the plots as above. One can of course play more with the options of SmoothKernelDistribution, depending on your application.


Comments