8000 DOC : added note about maintain ref to widgets · sudoboomboom/matplotlib@e4e9ee6 · GitHub
[go: up one dir, main page]

Skip to content

Commit e4e9ee6

Browse files
committed
DOC : added note about maintain ref to widgets
Added note to widget doc-strings that the user must maintain a reference to the widgets to keep them responsive (if they get gc'd the call backs go away). Some random conversion to numpyboc Closes matplotlib#3105
1 parent c4a014d commit e4e9ee6

File tree

1 file changed

+76
-31
lines changed

1 file changed

+76
-31
lines changed

lib/matplotlib/widgets.py

Lines changed: 76 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,15 @@ class Widget(object):
7070

7171

7272
class AxesWidget(Widget):
73-
"""Widget that is connected to a single :class:`~matplotlib.axes.Axes`.
73+
"""Widget that is connected to a single
74+
:class:`~matplotlib.axes.Axes`.
75+
76+
To guarantee that the widgets stay responsive maintain a reference
77+
to them. This is necessary because the callback registry
78+
maintains only weak-refs to the functions, which are member
79+
functions of the widget. If there are no references to the widget
80+
object it may be garbage collected which will disconnect the
81+
callbacks.
7482
7583
Attributes:
7684
@@ -112,7 +120,10 @@ def ignore(self, event):
112120

113121
class Button(AxesWidget):
114122
"""
115-
A GUI neutral button
123+
A GUI neutral button.
124+
125+
For the button to remain responsive
126+
you much keep a reference to it.
116127
117128
The following attributes are accessible
118129
@@ -134,22 +145,24 @@ class Button(AxesWidget):
134145
def __init__(self, ax, label, image=None,
135146
color='0.85', hovercolor='0.95'):
136147
"""
137-
*ax*
148+
Parameters
149+
----------
150+
ax : matplotlib.axes.Axes
138151
The :class:`matplotlib.axes.Axes` instance the button
139152
will be placed into.
140153
141-
*label*
154+
label : str
142155
The button text. Accepts string.
143156
144-
*image*
157+
image : array, mpl image, PIL image
145158
The image to place in the button, if not *None*.
146159
Can be any legal arg to imshow (numpy array,
147160
matplotlib Image instance, or PIL image).
148161
149-
*color*
162+
color : color
150163
The color of the button when not activated
151164
152-
*hovercolor*
165+
hovercolor : color
153166
The color of the button when the mouse is over it
154167
"""
155168
AxesWidget.__init__(self, ax)
@@ -233,7 +246,10 @@ def disconnect(self, cid):
233246

234247
class Slider(AxesWidget):
235248
"""
236-
A slider representing a floating point range
249+
A slider representing a floating point range.
250+
251+
For the slider
252+
to remain responsive you must maintain a reference to it.
237253
238254
The following attributes are defined
239255
*ax* : the slider :class:`matplotlib.axes.Axes` instance
@@ -269,28 +285,36 @@ def __init__(self, ax, label, valmin, valmax, valinit=0.5, valfmt='%1.2f',
269285
closedmin=True, closedmax=True, slidermin=None,
270286
slidermax=None, dragging=True, **kwargs):
271287
"""
272-
Create a slider from *valmin* to *valmax* in axes *ax*
288+
Create a slider from *valmin* to *valmax* in axes *ax*.
289+
290+
additional kwargs are passed on to ``self.poly`` which is the
291+
:class:`matplotlib.patches.Rectangle` which draws the slider
292+
knob. See the :class:`matplotlib.patches.Rectangle` documentation
293+
valid property names (e.g., *facecolor*, *edgecolor*, *alpha*, ...)
273294
274-
*valinit*
295+
Parameters
296+
----------
297+
valinit : float
275298
The slider initial position
276299
277-
*label*
300+
label : str
278301
The slider label
279302
280-
*valfmt*
281-
Used to format the slider value
303+
valfmt : str
304+
Used to format the slider value, fprint format string
282305
283-
*closedmin* and *closedmax*
306+
closedmin : bool
307+
closedmax : bool
284308
Indicate whether the slider interval is closed
285309
286-
*slidermin* and *slidermax*
310+
slidermin : float
311+
slidermax : float
287312
Used to constrain the value of this slider to the values
288313
of other sliders.
289314
290-
additional kwargs are passed on to ``self.poly`` which is the
291-
:class:`matplotlib.patches.Rectangle` which draws the slider
292-
knob. See the :class:`matplotlib.patches.Rectangle` documentation
293-
valid property names (e.g., *facecolor*, *edgecolor*, *alpha*, ...)
315+
dragging : bool
316+
if the silder can be dragged by the mouse
317+
294318
"""
295319
AxesWidget.__init__(self, ax)
296320

@@ -415,7 +439,10 @@ def reset(self):
415439

416440
class CheckButtons(AxesWidget):
417441
"""
418-
A GUI neutral radio button
442+
A GUI neutral radio button.
443+
444+
For the check buttons to remain responsive you much keep a
445+
reference to this object.
419446
420447
The following attributes are exposed
421448
@@ -549,6 +576,9 @@ class RadioButtons(AxesWidget):
549576
"""
550577
A GUI neutral radio button
551578
579+
For the buttons to remain responsive
580+
you much keep a reference to this object.
581+
552582
The following attributes are exposed
553583
554584
*ax*
@@ -832,7 +862,10 @@ class Cursor(AxesWidget):
832862
*vertOn*
833863
Controls the visibility of the horizontal line
834864
835-
and the visibility of the cursor itself with the *visible* attribute
865+
and the visibility of the cursor itself with the *visible* attribute.
866+
867+
For the cursor to remain responsive you much keep a reference to
868+
it.
836869
"""
837870
def __init__(self, ax, horizOn=True, vertOn=True, useblit=False,
838871
**lineprops):
@@ -914,7 +947,10 @@ def _update(self):
914947
class MultiCursor(Widget):
915948
"""
916949
Provide a vertical (default) and/or horizontal line cursor shared between
917-
multiple axes
950+
multiple axes.
951+
952+
For the cursor to remain responsive you much keep a reference to
953+
it.
918954
919955
Example usage::
920956
@@ -1027,7 +1063,10 @@ def _update(self):
10271063

10281064
class SpanSelector(AxesWidget):
10291065
"""
1030-
Select a min/max range of the x or y axes for a matplotlib Axes
1066+
Select a min/max range of the x or y axes for a matplotlib Axes.
1067+
1068+
For the selector to remain responsive you much keep a reference to
1069+
it.
10311070
10321071
Example usage::
10331072
@@ -1062,8 +1101,8 @@ def __init__(self, ax, onselect, direction, minspan=None, useblit=False,
10621101
10631102
Set the visible attribute to *False* if you want to turn off
10641103
the functionality of the span selector
1065-
1066-
If *span_stays* is True, the span stays visble after making
1104+
1105+
If *span_stays* is True, the span stays visble after making
10671106
a valid selection.
10681107
"""
10691108
AxesWidget.__init__(self, ax)
@@ -1085,7 +1124,7 @@ def __init__(self, ax, onselect, direction, minspan=None, useblit=False,
10851124
self.onmove_callback = onmove_callback
10861125
self.minspan = minspan
10871126
self.span_stays = span_stays
1088-
1127+
10891128
# Needed when dragging out of axes
10901129
self.buttonDown = False
10911130
self.prev = (0, 0)
@@ -1126,7 +1165,7 @@ def new_axes(self, ax):
11261165
visible=False,
11271166
**self.rectprops)
11281167
self.ax.add_patch(self.stay_rect)
1129-
1168+
11301169
if not self.useblit:
11311170
self.ax.add_patch(self.rect)
11321171

@@ -1152,7 +1191,7 @@ def press(self, event):
11521191
self.rect.set_visible(self.visible)
11531192
if self.span_stays:
11541193
self.stay_rect.set_visible(False)
1155-
1194+
11561195
if self.direction == 'horizontal':
11571196
self.pressv = event.xdata
11581197
else:
@@ -1168,14 +1207,14 @@ def release(self, event):
11681207
self.buttonDown = False
11691208

11701209
self.rect.set_visible(False)
1171-
1210+
11721211
if self.span_stays:
11731212
self.stay_rect.set_x(self.rect.get_x())
11741213
self.stay_rect.set_y(self.rect.get_y())
11751214
self.stay_rect.set_width(self.rect.get_width())
11761215
self.stay_rect.set_height(self.rect.get_height())
11771216
self.stay_rect.set_visible(True)
1178-
1217+
11791218
self.canvas.draw()
11801219
vmin = self.pressv
11811220
if self.direction == 'horizontal':
@@ -1245,7 +1284,10 @@ def onmove(self, event):
12451284

12461285
class RectangleSelector(AxesWidget):
12471286
"""
1248-
Select a min/max range of the x axes for a matplotlib Axes
1287+
Select a rectangular region of an axes.
1288+
1289+
For the cursor to remain responsive you much keep a reference to
1290+
it.
12491291
12501292
Example usage::
12511293
@@ -1528,6 +1570,9 @@ def get_active(self):
15281570
class LassoSelector(AxesWidget):
15291571
"""Selection curve of an arbitrary shape.
15301572
1573+
For the selector to remain responsive you much keep a reference to
1574+
it.
1575+
15311576
The selected path can be used in conjunction with
15321577
:func:`~matplotlib.path.Path.contains_point` to select
15331578
data points from an image.

0 commit comments

Comments
 (0)
0