@@ -94,14 +94,6 @@ class AxesWidget(Widget):
94
94
"""
95
95
Widget connected to a single `~matplotlib.axes.Axes`.
96
96
97
- To guarantee that the widget remains responsive and not garbage-collected,
98
- a reference to the object should be maintained by the user.
99
-
100
- This is necessary because the callback registry
101
- maintains only weak-refs to the functions, which are member
102
- functions of the widget. If there are no references to the widget
103
- object it may be garbage collected which will disconnect the callbacks.
104
-
105
97
Attributes
106
98
----------
107
99
ax : `~matplotlib.axes.Axes`
@@ -121,9 +113,11 @@ def connect_event(self, event, callback):
121
113
Connect callback with an event.
122
114
123
115
This should be used in lieu of `figure.canvas.mpl_connect` since this
124
- function stores callback ids for later clean up.
116
+ function stores callback ids for later clean up, and sets up a wrapper
117
+ callback to prevent the original callback and thus the widget from
118
+ being garbage collected.
125
119
"""
126
- cid = self .canvas .mpl_connect (event , callback )
120
+ cid = self .canvas .mpl_connect (event , lambda event : callback ( event ) )
127
121
self .cids .append (cid )
128
122
129
123
def disconnect_events (self ):
@@ -136,7 +130,6 @@ class Button(AxesWidget):
136
130
"""
137
131
A GUI neutral button.
138
132
139
- For the button to remain responsive you must keep a reference to it.
140
133
Call `.on_clicked` to connect to the button.
141
134
142
135
Attributes
@@ -247,9 +240,8 @@ class Slider(AxesWidget):
247
240
"""
248
241
A slider representing a floating point range.
249
242
250
- Create a slider from *valmin* to *valmax* in axes *ax*. For the slider to
251
- remain responsive you must maintain a reference to it. Call
252
- :meth:`on_changed` to connect to the slider event.
243
+ Create a slider from *valmin* to *valmax* in axes *ax*.
244
+ Call :meth:`on_changed` to connect to the slider event.
253
245
254
246
Attributes
255
247
----------
@@ -505,9 +497,6 @@ class CheckButtons(AxesWidget):
505
497
r"""
506
498
A GUI neutral set of check buttons.
507
499
508
- For the check buttons to remain responsive you must keep a
509
- reference to this object.
510
-
511
500
Connect to the CheckButtons with the `.on_clicked` method.
512
501
513
502
Attributes
@@ -660,8 +649,6 @@ class TextBox(AxesWidget):
660
649
"""
661
650
A GUI neutral text input box.
662
651
663
- For the text box to remain responsive you must keep a reference to it.
664
-
665
652
Call `.on_text_change` to be updated whenever the text changes.
666
653
667
654
Call `.on_submit` to be updated whenever the user hits enter or
@@ -968,9 +955,6 @@ class RadioButtons(AxesWidget):
968
955
"""
969
956
A GUI neutral radio button.
970
957
971
- For the buttons to remain responsive you must keep a reference to this
972
- object.
973
-
974
958
Connect to the RadioButtons with the `.on_clicked` method.
975
959
976
960
Attributes
@@ -1236,8 +1220,6 @@ class Cursor(AxesWidget):
1236
1220
"""
1237
1221
A crosshair cursor that spans the axes and moves with mouse cursor.
1238
1222
1239
- For the cursor to remain responsive you must keep a reference to it.
1240
-
1241
1223
Parameters
1242
1224
----------
1243
1225
ax : `matplotlib.axes.Axes`
@@ -1331,8 +1313,6 @@ class MultiCursor(Widget):
1331
1313
Provide a vertical (default) and/or horizontal line cursor shared between
1332
1314
multiple axes.
1333
1315
1334
- For the cursor to remain responsive you must keep a reference to it.
1335
-
1336
1316
Example usage::
1337
1317
1338
1318
from matplotlib.widgets import MultiCursor
@@ -1386,9 +1366,11 @@ def __init__(self, canvas, axes, useblit=True, horizOn=False, vertOn=True,
1386
1366
1387
1367
def connect (self ):
1388
1368
"""connect events"""
1389
- self ._cidmotion = self .canvas .mpl_connect ('motion_notify_event' ,
1390
- self .onmove )
1391
- self ._ciddraw = self .canvas .mpl_connect ('draw_event' , self .clear )
1369
+ # Wrapper lambdas prevent GC.
1370
+ self ._cidmotion = self .canvas .mpl_connect (
1371
+ 'motion_notify_event' , lambda event : self .onmove (event ))
1372
+ self ._ciddraw = self .canvas .mpl_connect (
1373
+ 'draw_event' , lambda event : self .clear (event ))
1392
1374
1393
1375
def disconnect (self ):
1394
1376
"""disconnect events"""
@@ -1656,8 +1638,6 @@ class SpanSelector(_SelectorWidget):
1656
1638
Visually select a min/max range on a single axis and call a function with
1657
1639
those values.
1658
1640
1659
- To guarantee that the selector remains responsive, keep a reference to it.
1660
-
1661
1641
In order to turn off the SpanSelector, set ``span_selector.active`` to
1662
1642
False. To turn it back on, set it to True.
1663
1643
@@ -1932,8 +1912,6 @@ class RectangleSelector(_SelectorWidget):
1932
1912
"""
1933
1913
Select a rectangular region of an axes.
1934
1914
1935
- For the cursor to remain responsive you must keep a reference to it.
1936
-
1937
1915
Example usage::
1938
1916
1939
1917
import numpy as np
@@ -2357,8 +2335,6 @@ class EllipseSelector(RectangleSelector):
2357
2335
"""
2358
2336
Select an elliptical region of an axes.
2359
2337
2360
- For the cursor to remain responsive you must keep a reference to it.
2361
-
2362
2338
Example usage::
2363
2339
2364
2340
import numpy as np
@@ -2427,8 +2403,6 @@ class LassoSelector(_SelectorWidget):
2427
2403
"""
2428
2404
Selection curve of an arbitrary shape.
2429
2405
2430
- For the selector to remain responsive you must keep a reference to it.
2431
-
2432
2406
The selected path can be used in conjunction with `~.Path.contains_point`
2433
2407
to select data points from an image.
2434
2408
@@ -2509,8 +2483,6 @@ class PolygonSelector(_SelectorWidget):
2509
2483
drag anywhere in the axes to move all vertices. Press the *esc* key to
2510
2484
start a new polygon.
2511
2485
2512
- For the selector to remain responsive you must keep a reference to it.
2513
-
2514
2486
Parameters
2515
2487
----------
2516
2488
ax : `~matplotlib.axes.Axes`
0 commit comments