@@ -955,24 +955,40 @@ def __init__(self, ax, onselect, **kwargs):
955955 warnings .warn ('Use SpanSelector instead!' , DeprecationWarning )
956956 SpanSelector .__init__ (self , ax , onselect , 'horizontal' , ** kwargs )
957957
958+
958959class RectangleSelector :
959960 """
960961 Select a min/max range of the x axes for a matplotlib Axes
961962
962963 Example usage:
963964
964- ax = subplot(111)
965- ax.plot(x,y)
965+ from matplotlib.widgets import RectangleSelector
966+ from pylab import *
966967
967- def onselect(eclick, erelease):
968+ def onselect(eclick, erelease):
968969 'eclick and erelease are matplotlib events at press and release'
969- print 'startposition : (%f,%f)'%(eclick.xdata, eclick.ydata)
970- print 'endposition : (%f,%f)'%(erelease.xdata, erelease.ydata)
971- print 'used button : ', eclick.button
972-
973- span = Selector(ax, onselect,drawtype='box')
974- show()
975-
970+ print ' startposition : (%f, %f)' % (eclick.xdata, eclick.ydata)
971+ print ' endposition : (%f, %f)' % (erelease.xdata, erelease.ydata)
972+ print ' used button : ', eclick.button
973+
974+ def toggle_Selector(event):
975+ print ' Key pressed.'
976+ if event.key in ['Q', 'q'] and toggle_Selector.RS.active:
977+ print ' RectangleSelector deactivated.'
978+ toggle_Selector.RS.set_active(False)
979+ if event.key in ['A', 'a'] and not toggle_Selector.RS.active:
980+ print ' RectangleSelector activated.'
981+ toggle_Selector.RS.set_active(True)
982+
983+ x = arange(100)/(99.0)
984+ y = sin(x)
985+ fig = figure
986+ ax = subplot(111)
987+ ax.plot(x,y)
988+
989+ toggle_Selector.RS = RectangleSelector(ax, onselect, drawtype='line')
990+ connect('key_press_event', toggle_Selector)
991+ show()
976992 """
977993 def __init__ (self , ax , onselect , drawtype = 'box' ,
978994 minspanx = None , minspany = None , useblit = False ,
@@ -1001,8 +1017,6 @@ def __init__(self, ax, onselect, drawtype='box',
10011017 Use type if you want the mouse to draw a line, a box or nothing
10021018 between click and actual position ny setting
10031019 drawtype = 'line', drawtype='box' or drawtype = 'none'.
1004-
1005-
10061020 """
10071021 self .ax = ax
10081022 self .visible = True
@@ -1012,6 +1026,7 @@ def __init__(self, ax, onselect, drawtype='box',
10121026 self .canvas .mpl_connect ('button_release_event' , self .release )
10131027 self .canvas .mpl_connect ('draw_event' , self .update_background )
10141028
1029+ self .active = True # for activation / deactivation
10151030 self .to_draw = None
10161031 self .background = None
10171032
@@ -1052,6 +1067,14 @@ def update_background(self, event):
10521067
10531068 def ignore (self , event ):
10541069 'return True if event should be ignored'
1070+ # If RectangleSelector is not active :
1071+ if not self .active :
1072+ return True
1073+
1074+ # If canvas was locked
1075+ if not self .canvas .widgetlock .available (self ):
1076+ return True
1077+
10551078 # If no button was pressed yet ignore the event if it was out
10561079 # of the axes
10571080 if self .eventpress == None :
@@ -1142,6 +1165,17 @@ def onmove(self, event):
11421165 self .update ()
11431166 return False
11441167
1168+ def set_active (self , active ):
1169+ """ Use this to activate / deactivate the RectangleSelector
1170+
1171+ from your program with an boolean variable 'active'.
1172+ """
1173+ self .active = active
1174+
1175+ def get_active (self ):
1176+ """ to get status of active mode (boolean variable)"""
1177+ return self .active
1178+
11451179class Lasso (Widget ):
11461180 def __init__ (self , ax , xy , callback = None , useblit = True ):
11471181 self .axes = ax
0 commit comments