44selected points
55
66This is currently a proof-of-concept implementation (though it is
7- usable as is). There will be some refinement of the API and the
8- inside polygon detection routine.
7+ usable as is). There will be some refinement of the API.
98"""
109from matplotlib .widgets import Lasso
11- from matplotlib .nxutils import points_inside_poly
1210from matplotlib .colors import colorConverter
1311from matplotlib .collections import RegularPolyCollection
12+ from matplotlib import path
1413
15- from matplotlib .pyplot import figure , show
14+ import matplotlib .pyplot as plt
1615from numpy import nonzero
1716from numpy .random import rand
1817
19- class Datum :
18+ class Datum ( object ) :
2019 colorin = colorConverter .to_rgba ('red' )
21- colorout = colorConverter .to_rgba ('green ' )
20+ colorout = colorConverter .to_rgba ('blue ' )
2221 def __init__ (self , x , y , include = False ):
2322 self .x = x
2423 self .y = y
2524 if include : self .color = self .colorin
2625 else : self .color = self .colorout
2726
2827
29- class LassoManager :
28+ class LassoManager ( object ) :
3029 def __init__ (self , ax , data ):
3130 self .axes = ax
3231 self .canvas = ax .figure .canvas
@@ -46,21 +45,21 @@ def __init__(self, ax, data):
4645 ax .add_collection (self .collection )
4746
4847 self .cid = self .canvas .mpl_connect ('button_press_event' , self .onpress )
49- self .ind = None
5048
5149 def callback (self , verts ):
5250 facecolors = self .collection .get_facecolors ()
53- ind = nonzero (points_inside_poly (self .xys , verts ))[0 ]
54- for i in range (self .Nxy ):
55- if i in ind :
51+ p = path .Path (verts )
52+ ind = p .contains_points (self .xys )
53+ for i in range (len (self .xys )):
54+ if ind [i ]:
5655 facecolors [i ] = Datum .colorin
5756 else :
5857 facecolors [i ] = Datum .colorout
5958
6059 self .canvas .draw_idle ()
6160 self .canvas .widgetlock .release (self .lasso )
6261 del self .lasso
63- self . ind = ind
62+
6463 def onpress (self , event ):
6564 if self .canvas .widgetlock .locked (): return
6665 if event .inaxes is None : return
@@ -72,8 +71,7 @@ def onpress(self, event):
7271
7372 data = [Datum (* xy ) for xy in rand (100 , 2 )]
7473
75- fig = figure ()
76- ax = fig .add_subplot (111 , xlim = (0 ,1 ), ylim = (0 ,1 ), autoscale_on = False )
74+ ax = plt .axes (xlim = (0 ,1 ), ylim = (0 ,1 ), autoscale_on = False )
7775 lman = LassoManager (ax , data )
7876
79- show ()
77+ plt . show ()
0 commit comments