## Interactively extract points from a plot (ListPlot or SmoothDensityHistogram)

20

13

Is there a way to dynamically define a polygon on a plot (I'm working with ListPlot and SmoothDensityHistogram) to select a cluster of interest, and give the positions of those points in the original list of data?

I'd appreciate any help!

Here's just an example set of points:

x = {
{RandomReal[{0, 5}, 20],
RandomReal[{4, 4.5}, 10]},
{RandomReal[1, 20],
RandomReal[{1.5, 2}, 10]}
};

points = Transpose[Join @@@ x] ~RandomSample~ 30;

SmoothDensityHistogram[points, ColorFunction -> "TemperatureMap"]
ListPlot[points, PlotRange -> {{0, 5.5}, {0, 2.5}}]


3

Press CTRL-D to open the drawing tools, then draw a polygon. Select the polygon, copy it, and paste it back into an input cell to get the vertex coordinates. Then filter the points based on whether they're in the polygon. Someone will probably write a Manipulate with a Paste button to do this automatically.

– Szabolcs – 2012-03-27T11:40:18.313

19

This is basically the same as what b.gatessucks is doing. The main addition is that I've put all the locators in one list. To add vertices to the polygon you just click somewhere on the graph. I've also added a reset button and a button that prints the indices of the points inside the polygon which makes it easier to copy.

points = RandomSample[
Transpose[{Flatten[{RandomReal[{0, 5}, 20], RandomReal[{4, 4.5}, 10]}],
Flatten[{RandomReal[1, 20], RandomReal[{1.5, 2}, 10]}]}], 30];

winding[poly_, pt_] := Round[(Total @ Mod[(# - RotateRight[#]) &@
(ArcTan @@ (pt - #) & /@ poly), 2 Pi, -Pi]/2/Pi)]

DynamicModule[{pl, pos},
pl = SmoothDensityHistogram[points, ColorFunction -> "TemperatureMap"];
Manipulate[
pos = Pick[Range[Length[points]], Unitize[winding[poly, #] & /@ points], 1];
Show[pl,
Epilog -> {{Darker[Green], PointSize[Medium], Point[points[[pos]]]},
{Black, Point[Complement[points, points[[pos]]]]},
{EdgeForm[{Red, Dashed}], FaceForm[], Polygon[poly]}}],

{{poly, {}}, Locator, LocatorAutoCreate -> All},
Row[{Button["Copy Points", Print[pos]], Button["Reset", poly = {}; pos = {}]}]]]


@SparsePine and an "undo" button is also useful, Button["undo", poly = Drop[poly, -1]] – matheorem – 2013-12-11T04:39:50.993

Hi, Heike, Could you please tell me how to change Locator size in manipulate? – matheorem – 2013-12-11T06:46:12.197

@Heike I suppose you can also have have a button: Button["Get Polytope Vertices", Print[poly]], which returns the vertices of the polytope you've defined by clicking. – Sparse Pine – 2013-07-27T03:54:26.580

12

Something like (@Szabolcs provided the link to PointInPoly) :

Manipulate[
Column[{
Show[ListPlot[points, PlotRange -> {{0, 5.5}, {0, 2.5}}],
Graphics[{Pink, Opacity[0.5], Polygon[{p1, p2, p3, p4}]}]],
Position[points, #] & /@ Select[points, PointInPoly[#, {p1, p2, p3, p4}] == 1 &]}],
{{p1, {0, 0}}, Locator},
{{p2, {3, 1}}, Locator},
{{p3, {1, 1}}, Locator},
{{p4, {2, 1}}, Locator}]


Thank you. This was very helpful. – Daniel S – 2012-03-27T13:40:53.840

It would probably be more convenient to actually have the code for PointInPoly here... kind of bloats, but makes evaluating that much more comfortable. – Yves Klett – 2012-03-28T08:40:19.490