Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 30ee939

Browse files
authored
Merge pull request #15237 from anntzer/widgets
Cleanup widgets.py.
2 parents 6675e8c + 613111c commit 30ee939

File tree

1 file changed

+48
-87
lines changed

1 file changed

+48
-87
lines changed

lib/matplotlib/widgets.py

Lines changed: 48 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -78,30 +78,28 @@ def get_active(self):
7878
return self._active
7979

8080
# set_active is overridden by SelectorWidgets.
81-
active = property(get_active, lambda self, active: self.set_active(active),
82-
doc="Is the widget active?")
81+
active = property(get_active, set_active, doc="Is the widget active?")
8382

8483
def ignore(self, event):
85-
"""Return True if event should be ignored.
84+
"""
85+
Return whether *event* should be ignored.
8686
87-
This method (or a version of it) should be called at the beginning
88-
of any event callback.
87+
This method should be called at the beginning of any event callback.
8988
"""
9089
return not self.active
9190

9291

9392
class AxesWidget(Widget):
94-
"""Widget that is connected to a single
95-
:class:`~matplotlib.axes.Axes`.
93+
"""
94+
Widget that is connected to a single `~matplotlib.axes.Axes`.
9695
9796
To guarantee that the widget remains responsive and not garbage-collected,
9897
a reference to the object should be maintained by the user.
9998
10099
This is necessary because the callback registry
101100
maintains only weak-refs to the functions, which are member
102101
functions of the widget. If there are no references to the widget
103-
object it may be garbage collected which will disconnect the
104-
callbacks.
102+
object it may be garbage collected which will disconnect the callbacks.
105103
106104
Attributes
107105
----------
@@ -118,7 +116,8 @@ def __init__(self, ax):
118116
self.cids = []
119117

120118
def connect_event(self, event, callback):
121-
"""Connect callback with an event.
119+
"""
120+
Connect callback with an event.
122121
123122
This should be used in lieu of `figure.canvas.mpl_connect` since this
124123
function stores callback ids for later clean up.
@@ -137,7 +136,7 @@ class Button(AxesWidget):
137136
A GUI neutral button.
138137
139138
For the button to remain responsive you must keep a reference to it.
140-
Call :meth:`on_clicked` to connect to the button.
139+
Call `.on_clicked` to connect to the button.
141140
142141
Attributes
143142
----------
@@ -158,18 +157,13 @@ def __init__(self, ax, label, image=None,
158157
----------
159158
ax : `~matplotlib.axes.Axes`
160159
The `~.axes.Axes` instance the button will be placed into.
161-
162160
label : str
163161
The button text. Accepts string.
164-
165-
image : array, mpl image, Pillow Image
162+
image : array-like or PIL image
166163
The image to place in the button, if not *None*.
167-
Can be any legal arg to imshow (numpy array,
168-
matplotlib Image instance, or Pillow Image).
169-
164+
Supported inputs are the same as for `.Axes.imshow`.
170165
color : color
171166
The color of the button when not activated.
172-
173167
hovercolor : color
174168
The color of the button when the mouse is over it.
175169
"""
@@ -198,24 +192,20 @@ def __init__(self, ax, label, image=None,
198192
self._lastcolor = color
199193

200194
def _click(self, event):
201-
if self.ignore(event):
202-
return
203-
if event.inaxes != self.ax:
204-
return
205-
if not self.eventson:
195+
if (self.ignore(event)
196+
or event.inaxes != self.ax
197+
or not self.eventson):
206198
return
207199
if event.canvas.mouse_grabber != self.ax:
208200
event.canvas.grab_mouse(self.ax)
209201

210202
def _release(self, event):
211-
if self.ignore(event):
212-
return
213-
if event.canvas.mouse_grabber != self.ax:
203+
if (self.ignore(event)
204+
or event.canvas.mouse_grabber != self.ax):
214205
return
215206
event.canvas.release_mouse(self.ax)
216-
if not self.eventson:
217-
return
218-
if event.inaxes != self.ax:
207+
if (not self.eventson
208+
or event.inaxes != self.ax):
219209
return
220210
for cid, func in self.observers.items():
221211
func(event)
@@ -622,9 +612,8 @@ def set_active(self, index):
622612
Raises ValueError if *index* is invalid.
623613
624614
Callbacks will be triggered if :attr:`eventson` is True.
625-
626615
"""
627-
if 0 > index >= len(self.labels):
616+
if not 0 <= index < len(self.labels):
628617
raise ValueError("Invalid CheckButton index: %d" % index)
629618

630619
l1, l2 = self.lines[index]
@@ -823,12 +812,12 @@ def _keypress(self, event):
823812
self.cursor_index = 0
824813
elif key == "end":
825814
self.cursor_index = len(self.text)
826-
elif(key == "backspace"):
815+
elif key == "backspace":
827816
if self.cursor_index != 0:
828817
self.text = (self.text[:self.cursor_index - 1] +
829818
self.text[self.cursor_index:])
830819
self.cursor_index -= 1
831-
elif(key == "delete"):
820+
elif key == "delete":
832821
if self.cursor_index != len(self.text):
833822
self.text = (self.text[:self.cursor_index] +
834823
self.text[self.cursor_index + 1:])
@@ -871,9 +860,8 @@ def begin_typing(self, x):
871860

872861
def stop_typing(self):
873862
notifysubmit = False
874-
# because _notify_submit_users might throw an error in the
875-
# user's code, we only want to call it once we've already done
876-
# our cleanup.
863+
# Because _notify_submit_users might throw an error in the user's code,
864+
# we only want to call it once we've already done our cleanup.
877865
if self.capturekeystrokes:
878866
# Check for toolmanager handling the keypress
879867
if self.ax.figure.canvas.manager.key_press_handler_id is not None:
@@ -988,7 +976,6 @@ class RadioButtons(AxesWidget):
988976
989977
Connect to the RadioButtons with the :meth:`on_clicked` method.
990978
991-
992979
Attributes
993980
----------
994981
ax
@@ -1001,8 +988,8 @@ class RadioButtons(AxesWidget):
1001988
A list of `~.patches.Circle` instances defining the buttons.
1002989
value_selected : str
1003990
The label text of the currently selected button.
1004-
1005991
"""
992+
1006993
def __init__(self, ax, labels, active=0, activecolor='blue'):
1007994
"""
1008995
Add radio buttons to an `~.axes.Axes`.
@@ -1348,8 +1335,7 @@ class MultiCursor(Widget):
13481335
Provide a vertical (default) and/or horizontal line cursor shared between
13491336
multiple axes.
13501337
1351-
For the cursor to remain responsive you must keep a reference to
1352-
it.
1338+
For the cursor to remain responsive you must keep a reference to it.
13531339
13541340
Example usage::
13551341
@@ -1511,54 +1497,42 @@ def connect_default_events(self):
15111497
self.connect_event('scroll_event', self.on_scroll)
15121498

15131499
def ignore(self, event):
1514-
"""return *True* if *event* should be ignored"""
1500+
# docstring inherited
15151501
if not self.active or not self.ax.get_visible():
15161502
return True
1517-
15181503
# If canvas was locked
15191504
if not self.canvas.widgetlock.available(self):
15201505
return True
1521-
15221506
if not hasattr(event, 'button'):
15231507
event.button = None
1524-
15251508
# Only do rectangle selection if event was triggered
15261509
# with a desired button
1527-
if self.validButtons is not None:
1528-
if event.button not in self.validButtons:
1529-
return True
1530-
1510+
if (self.validButtons is not None
1511+
and event.button not in self.validButtons):
1512+
return True
15311513
# If no button was pressed yet ignore the event if it was out
15321514
# of the axes
15331515
if self.eventpress is None:
15341516
return event.inaxes != self.ax
1535-
1536-
# If a button was pressed, check if the release-button is the
1537-
# same.
1517+
# If a button was pressed, check if the release-button is the same.
15381518
if event.button == self.eventpress.button:
15391519
return False
1540-
1541-
# If a button was pressed, check if the release-button is the
1542-
# same.
1520+
# If a button was pressed, check if the release-button is the same.
15431521
return (event.inaxes != self.ax or
15441522
event.button != self.eventpress.button)
15451523

15461524
def update(self):
1547-
"""draw using newfangled blit or oldfangled draw depending on
1548-
useblit
1549-
1525+
"""
1526+
Draw using blit() or draw_idle() depending on ``self.useblit``.
15501527
"""
15511528
if not self.ax.get_visible():
15521529
return False
1553-
15541530
if self.useblit:
15551531
if self.background is not None:
15561532
self.canvas.restore_region(self.background)
15571533
for artist in self.artists:
15581534
self.ax.draw_artist(artist)
1559-
15601535
self.canvas.blit(self.ax.bbox)
1561-
15621536
else:
15631537
self.canvas.draw_idle()
15641538
return False
@@ -1608,7 +1582,6 @@ def press(self, event):
16081582

16091583
def _press(self, event):
16101584
"""Button press handler"""
1611-
pass
16121585

16131586
def release(self, event):
16141587
"""Button release event handler and validator"""
@@ -1624,7 +1597,6 @@ def release(self, event):
16241597

16251598
def _release(self, event):
16261599
"""Button release event handler"""
1627-
pass
16281600

16291601
def onmove(self, event):
16301602
"""Cursor move event handler and validator"""
@@ -1636,7 +1608,6 @@ def onmove(self, event):
16361608

16371609
def _onmove(self, event):
16381610
"""Cursor move event handler"""
1639-
pass
16401611

16411612
def on_scroll(self, event):
16421613
"""Mouse scroll event handler and validator"""
@@ -1645,7 +1616,6 @@ def on_scroll(self, event):
16451616

16461617
def _on_scroll(self, event):
16471618
"""Mouse scroll event handler"""
1648-
pass
16491619

16501620
def on_key_press(self, event):
16511621
"""Key press event handler and validator for all selection widgets"""
@@ -1665,7 +1635,6 @@ def on_key_press(self, event):
16651635
def _on_key_press(self, event):
16661636
"""Key press event handler - use for widget-specific key press actions.
16671637
"""
1668-
pass
16691638

16701639
def on_key_release(self, event):
16711640
"""Key release event handler and validator."""
@@ -1742,7 +1711,6 @@ class SpanSelector(_SelectorWidget):
17421711
>>> fig.show()
17431712
17441713
See also: :doc:`/gallery/widgets/span_selector`
1745-
17461714
"""
17471715

17481716
def __init__(self, ax, onselect, direction, minspan=None, useblit=False,
@@ -1776,7 +1744,7 @@ def __init__(self, ax, onselect, direction, minspan=None, useblit=False,
17761744
self.new_axes(ax)
17771745

17781746
def new_axes(self, ax):
1779-
"""Set SpanSelector to operate on a new Axes"""
1747+
"""Set SpanSelector to operate on a new Axes."""
17801748
self.ax = ax
17811749
if self.canvas is not ax.figure.canvas:
17821750
if self.canvas is not None:
@@ -1809,7 +1777,7 @@ def new_axes(self, ax):
18091777
self.artists = [self.rect]
18101778

18111779
def ignore(self, event):
1812-
"""return *True* if *event* should be ignored"""
1780+
# docstring inherited
18131781
return _SelectorWidget.ignore(self, event) or not self.visible
18141782

18151783
def _press(self, event):
@@ -1912,7 +1880,8 @@ def _set_span_xy(self, event):
19121880

19131881

19141882
class ToolHandles:
1915-
"""Control handles for canvas tools.
1883+
"""
1884+
Control handles for canvas tools.
19161885
19171886
Parameters
19181887
----------
@@ -1928,7 +1897,6 @@ class ToolHandles:
19281897

19291898
def __init__(self, ax, x, y, marker='o', marker_props=None, useblit=True):
19301899
self.ax = ax
1931-
19321900
props = dict(marker=marker, markersize=7, mfc='w', ls='none',
19331901
alpha=0.5, visible=False, label='_nolegend_')
19341902
props.update(marker_props if marker_props is not None else {})
@@ -1972,8 +1940,7 @@ class RectangleSelector(_SelectorWidget):
19721940
"""
19731941
Select a rectangular region of an axes.
19741942
1975-
For the cursor to remain responsive you must keep a reference to
1976-
it.
1943+
For the cursor to remain responsive you must keep a reference to it.
19771944
19781945
Example usage::
19791946
@@ -2080,9 +2047,9 @@ def __init__(self, ax, onselect, drawtype='box',
20802047
self.visible = True
20812048
self.interactive = interactive
20822049

2083-
if drawtype == 'none':
2084-
drawtype = 'line' # draw a line but make it
2085-
self.visible = False # invisible
2050+
if drawtype == 'none': # draw a line but make it invisible
2051+
drawtype = 'line'
2052+
self.visible = False
20862053

20872054
if drawtype == 'box':
20882055
if rectprops is None:
@@ -2400,8 +2367,7 @@ class EllipseSelector(RectangleSelector):
24002367
"""
24012368
Select an elliptical region of an axes.
24022369
2403-
For the cursor to remain responsive you must keep a reference to
2404-
it.
2370+
For the cursor to remain responsive you must keep a reference to it.
24052371
24062372
Example usage::
24072373
@@ -2507,7 +2473,6 @@ def onselect(verts):
25072473
- 1 = left mouse button
25082474
- 2 = center mouse button (scroll wheel)
25092475
- 3 = right mouse button
2510-
25112476
"""
25122477

25132478
def __init__(self, ax, onselect=None, useblit=True, lineprops=None,
@@ -2555,7 +2520,8 @@ def _onmove(self, event):
25552520

25562521

25572522
class PolygonSelector(_SelectorWidget):
2558-
"""Select a polygon region of an axes.
2523+
"""
2524+
Select a polygon region of an axes.
25592525
25602526
Place vertices with each mouse click, and make the selection by completing
25612527
the polygon (clicking on the first vertex). Hold the *ctrl* key and click
@@ -2757,18 +2723,13 @@ def _draw_polygon(self):
27572723

27582724
@property
27592725
def verts(self):
2760-
"""Get the polygon vertices.
2761-
2762-
Returns
2763-
-------
2764-
list
2765-
A list of the vertices of the polygon as ``(xdata, ydata)`` tuples.
2766-
"""
2726+
"""The polygon vertices, as a list of ``(x, y)`` pairs."""
27672727
return list(zip(self._xs[:-1], self._ys[:-1]))
27682728

27692729

27702730
class Lasso(AxesWidget):
2771-
"""Selection curve of an arbitrary shape.
2731+
"""
2732+
Selection curve of an arbitrary shape.
27722733
27732734
The selected path can be used in conjunction with
27742735
:func:`~matplotlib.path.Path.contains_point` to select data points

0 commit comments

Comments
 (0)