Mathematica Stack Exchange is a question and answer site for users of Mathematica. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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}}]
share|improve this question
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 Mar 27 '12 at 11:40
up vote 18 down vote accepted

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"];
  pos = Pick[Range[Length[points]], Unitize[winding[poly, #] & /@ points], 1];
   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 = {}]}]]]

Mathematica graphics

share|improve this answer
Thanks - this is fantastic! – Daniel S Mar 27 '12 at 13:39
@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 Jul 27 '13 at 3:54
@SparsePine and an "undo" button is also useful, Button["undo", poly = Drop[poly, -1]] – matheorem Dec 11 '13 at 4:39
Hi, Heike, Could you please tell me how to change Locator size in manipulate? – matheorem Dec 11 '13 at 6:46

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

    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}]

enter image description here

share|improve this answer
Thank you. This was very helpful. – Daniel S Mar 27 '12 at 13:40
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 Mar 28 '12 at 8:40

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.