@@ -95,7 +95,7 @@ def __init__(self, ax):
95
95
self .ax = ax
96
96
self .canvas = ax .figure .canvas
97
97
self .cids = []
98
- self .active = True
98
+ self ._active = True
99
99
100
100
def connect_event (self , event , callback ):
101
101
"""Connect callback with an event.
@@ -111,6 +111,20 @@ def disconnect_events(self):
111
111
for c in self .cids :
112
112
self .canvas .mpl_disconnect (c )
113
113
114
+ def set_active (self , active ):
115
+ """Set whether the widget is active.
116
+ """
117
+ self ._active = active
118
+
119
+ def get_active (self ):
120
+ """Get whether the widget is active.
121
+ """
122
+ return self ._active
123
+
124
+ # set_active is overriden by SelectorWidgets.
125
+ active = property (get_active , lambda self , active : self .set_active (active ),
126
+ doc = "Is the widget active?" )
127
+
114
128
def ignore (self , event ):
115
129
"""Return True if event should be ignored.
116
130
@@ -1081,7 +1095,21 @@ def _update(self):
1081
1095
self .canvas .draw_idle ()
1082
1096
1083
1097
1084
- class SpanSelector (AxesWidget ):
1098
+ class _SelectorWidget (AxesWidget ):
1099
+ def set_active (self , active ):
1100
+ AxesWidget .set_active (self , active )
1101
+ if active :
1102
+ self .update_background (None )
1103
+
1104
+ def update_background (self , event ):
1105
+ """force an update of the background"""
1106
+ # If you add a call to `ignore` here, you'll want to check edge case:
1107
+ # `release` can call a draw event even when `ignore` is True.
1108
+ if self .useblit :
1109
+ self .background = self .canvas .copy_from_bbox (self .ax .bbox )
1110
+
1111
+
1112
+ class SpanSelector (_SelectorWidget ):
1085
1113
"""
1086
1114
Select a min/max range of the x or y axes for a matplotlib Axes.
1087
1115
@@ -1189,13 +1217,6 @@ def new_axes(self, ax):
1189
1217
if not self .useblit :
1190
1218
self .ax .add_patch (self .rect )
1191
1219
1192
- def update_background (self , event ):
1193
- """force an update of the background"""
1194
- # If you add a call to `ignore` here, you'll want to check edge case:
1195
- # `release` can call a draw event even when `ignore` is True.
1196
- if self .useblit :
1197
- self .background = self .canvas .copy_from_bbox (self .ax .bbox )
1198
-
1199
1220
def ignore (self , event ):
1200
1221
"""return *True* if *event* should be ignored"""
1201
1222
widget_off = not self .visible or not self .active
@@ -1302,7 +1323,7 @@ def onmove(self, event):
1302
1323
return False
1303
1324
1304
1325
1305
- class RectangleSelector (AxesWidget ):
1326
+ class RectangleSelector (_SelectorWidget ):
1306
1327
"""
1307
1328
Select a rectangular region of an axes.
1308
1329
@@ -1393,7 +1414,6 @@ def __init__(self, ax, onselect, drawtype='box',
1393
1414
self .connect_event ('button_release_event' , self .release )
1394
1415
self .connect_event ('draw_event' , self .update_background )
1395
1416
1396
- self .active = True # for activation / deactivation
1397
1417
self .to_draw = None
1398
1418
self .background = None
1399
1419
@@ -1437,11 +1457,6 @@ def __init__(self, ax, onselect, drawtype='box',
1437
1457
# will save the data (pos. at mouserelease)
1438
1458
self .eventrelease = None
1439
1459
1440
- def update_background (self , event ):
1441
- """force an update of the background"""
1442
- if self .useblit :
1443
- self .background = self .canvas .copy_from_bbox (self .ax .bbox )
1444
-
1445
1460
def ignore (self , event ):
1446
1461
"""return *True* if *event* should be ignored"""
1447
1462
if not self .active :
@@ -1575,19 +1590,8 @@ def onmove(self, event):
1575
1590
self .update ()
1576
1591
return False
1577
1592
1578
- def set_active (self , active ):
1579
- """
1580
- Use this to activate / deactivate the RectangleSelector
1581
- from your program with an boolean parameter *active*.
1582
- """
1583
- self .active = active
1584
-
1585
- def get_active (self ):
1586
- """ Get status of active mode (boolean variable)"""
1587
- return self .active
1588
1593
1589
-
1590
- class LassoSelector (AxesWidget ):
1594
+ class LassoSelector (_SelectorWidget ):
1591
1595
"""Selection curve of an arbitrary shape.
1592
1596
1593
1597
For the selector to remain responsive you much keep a reference to
@@ -1679,12 +1683,6 @@ def onmove(self, event):
1679
1683
else :
1680
1684
self .canvas .draw_idle ()
1681
1685
1682
- def update_background (self , event ):
1683
- if self .ignore (event ):
1684
- return
1685
- if self .useblit :
1686
- self .background = self .canvas .copy_from_bbox (self .ax .bbox )
1687
-
1688
1686
1689
1687
class Lasso (AxesWidget ):
1690
1688
"""Selection curve of an arbitrary shape.
0 commit comments