Message Boards Message Boards

Selecting Data Points from a Plot as input for further calculations

Hello all,

I was wondering if there is a way, in Mathematica, to use the mouse to select several points which creates an irregular polygon within a Plot which can act as a bounding box to select all of the data within that bounding box. I would like this process to be relatively automated, such that once the user selects the bounding area, Mathematica would automatically use the points within the selected area to perform a series of calculations and ultimately generate a new plot. Can I use the Input[] function for this? Is it even possible to select points on a plot using an irregular polygon? If so, how would you signal to Mathematica that you are done drawing your box?

Thanks for any help that you can provide.


POSTED BY: Matty Mookerjee
2 Replies

Matty, thank you very much for actually coming back and posting your finds. Both solutions on your link look so good and useful that I will repost them here.


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


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

enter image description here

POSTED BY: Sam Carrettie

Hi all,

I thought I should share a solution to the question I posed, which I found while searching the inter-webs.

Here's the link:

Interactively extract points from a plot (ListPlot or SmoothDensityHistogram)

The user, Heiki, wrote a script that does, pretty much, exactly what I was looking for.

I hope this helps others as well.

Take care,


POSTED BY: Matty Mookerjee
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
or Discard

Group Abstract Group Abstract