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
1110from matplotlib .colors import colorConverter
1211from matplotlib .collections import RegularPolyCollection
1312from 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,22 +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 ()
5351 p = path .Path (verts )
54- ind = nonzero ( p .contains_points (self .xys ))[ 0 ]
55- for i in range (self .Nxy ):
56- if i in ind :
52+ ind = p .contains_points (self .xys )
53+ for i in range (len ( self .xys ) ):
54+ if ind [ i ] :
5755 facecolors [i ] = Datum .colorin
5856 else :
5957 facecolors [i ] = Datum .colorout
6058
6159 self .canvas .draw_idle ()
6260 self .canvas .widgetlock .release (self .lasso )
6361 del self .lasso
64- self . ind = ind
62+
6563 def onpress (self , event ):
6664 if self .canvas .widgetlock .locked (): return
6765 if event .inaxes is None : return
@@ -73,8 +71,7 @@ def onpress(self, event):
7371
7472 data = [Datum (* xy ) for xy in rand (100 , 2 )]
7573
76- fig = figure ()
77- 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 )
7875 lman = LassoManager (ax , data )
7976
80- show ()
77+ plt . show ()
0 commit comments