Skip to content

Commit 190c96c

Browse files
committed
Use _selection_artist property
1 parent 6a93943 commit 190c96c

File tree

1 file changed

+52
-47
lines changed

1 file changed

+52
-47
lines changed

lib/matplotlib/widgets.py

+52-47
Original file line numberDiff line numberDiff line change
@@ -2013,15 +2013,20 @@ def add_artist(self, artist):
20132013

20142014
@property
20152015
def artists(self):
2016-
"""Tuple of the artists of the selector"""
2016+
"""Tuple of the artists of the selector."""
20172017
return tuple(self._artists)
20182018

2019+
@property
2020+
def _selection_artist(self):
2021+
"""The selection artist."""
2022+
return self.artists[0]
2023+
20192024
def set_props(self, **props):
20202025
"""
2021-
Set the properties of the selector artist. See the `props` argument
2026+
Set the properties of the selection artist. See the `props` argument
20222027
in the selector docstring to know which properties are supported.
20232028
"""
2024-
self.artists[0].set(**props)
2029+
self._selection_artist.set(**props)
20252030
self.update()
20262031
self._props = props
20272032

@@ -2168,7 +2173,7 @@ def __init__(self, ax, onselect, direction, minspan=0, useblit=False,
21682173
self._prev = (0, 0)
21692174

21702175
rect = _api.deprecated("3.5")(
2171-
property(lambda self: self.artists[0])
2176+
property(lambda self: self._selection_artist)
21722177
)
21732178

21742179
rectprops = _api.deprecated("3.5")(
@@ -2244,7 +2249,7 @@ def connect_default_events(self):
22442249
def _press(self, event):
22452250
"""Button press event handler."""
22462251
self._set_cursor(True)
2247-
if self._interactive and self.artists[0].get_visible():
2252+
if self._interactive and self._selection_artist.get_visible():
22482253
self._set_active_handle(event)
22492254
else:
22502255
self._active_handle = None
@@ -2300,7 +2305,7 @@ def _release(self, event):
23002305
"""Button release event handler."""
23012306
self._set_cursor(False)
23022307
if not self._interactive:
2303-
self.artists[0].set_visible(False)
2308+
self._selection_artist.set_visible(False)
23042309

23052310
vmin, vmax = self.extents
23062311
span = vmax - vmin
@@ -2377,11 +2382,11 @@ def _draw_shape(self, vmin, vmax):
23772382
if vmin > vmax:
23782383
vmin, vmax = vmax, vmin
23792384
if self.direction == 'horizontal':
2380-
self.artists[0].set_x(vmin)
2381-
self.artists[0].set_width(vmax - vmin)
2385+
self._selection_artist.set_x(vmin)
2386+
self._selection_artist.set_width(vmax - vmin)
23822387
else:
2383-
self.artists[0].set_y(vmin)
2384-
self.artists[0].set_height(vmax - vmin)
2388+
self._selection_artist.set_y(vmin)
2389+
self._selection_artist.set_height(vmax - vmin)
23852390

23862391
def _set_active_handle(self, event):
23872392
"""Set active handle based on the location of the mouse event."""
@@ -2411,17 +2416,17 @@ def _set_active_handle(self, event):
24112416

24122417
def _contains(self, event):
24132418
"""Return True if event is within the patch."""
2414-
return self.artists[0].contains(event, radius=0)[0]
2419+
return self._selection_artist.contains(event, radius=0)[0]
24152420

24162421
@property
24172422
def extents(self):
24182423
"""Return extents of the span selector."""
24192424
if self.direction == 'horizontal':
2420-
vmin = self.artists[0].get_x()
2421-
vmax = vmin + self.artists[0].get_width()
2425+
vmin = self._selection_artist.get_x()
2426+
vmax = vmin + self._selection_artist.get_width()
24222427
else:
2423-
vmin = self.artists[0].get_y()
2424-
vmax = vmin + self.artists[0].get_height()
2428+
vmin = self._selection_artist.get_y()
2429+
vmax = vmin + self._selection_artist.get_height()
24252430
return vmin, vmax
24262431

24272432
@extents.setter
@@ -2802,7 +2807,7 @@ def __init__(self, ax, onselect, drawtype='box',
28022807
self._extents_on_press = None
28032808

28042809
to_draw = _api.deprecated("3.5")(
2805-
property(lambda self: self.artists[0])
2810+
property(lambda self: self._selection_artist)
28062811
)
28072812

28082813
drawtype = _api.deprecate_privatize_attribute("3.5")
@@ -2824,7 +2829,7 @@ def _press(self, event):
28242829
"""Button press event handler."""
28252830
# make the drawn box/line visible get the click-coordinates,
28262831
# button, ...
2827-
if self._interactive and self.artists[0].get_visible():
2832+
if self._interactive and self._selection_artist.get_visible():
28282833
self._set_active_handle(event)
28292834
else:
28302835
self._active_handle = None
@@ -2847,7 +2852,7 @@ def _press(self, event):
28472852
def _release(self, event):
28482853
"""Button release event handler."""
28492854
if not self._interactive:
2850-
self.artists[0].set_visible(False)
2855+
self._selection_artist.set_visible(False)
28512856

28522857
# update the eventpress and eventrelease with the resulting extents
28532858
x0, x1, y0, y1 = self.extents
@@ -2947,13 +2952,13 @@ def _onmove(self, event):
29472952
@property
29482953
def _rect_bbox(self):
29492954
if self._drawtype == 'box':
2950-
x0 = self.artists[0].get_x()
2951-
y0 = self.artists[0].get_y()
2952-
width = self.artists[0].get_width()
2953-
height = self.artists[0].get_height()
2955+
x0 = self._selection_artist.get_x()
2956+
y0 = self._selection_artist.get_y()
2957+
width = self._selection_artist.get_width()
2958+
height = self._selection_artist.get_height()
29542959
return x0, y0, width, height
29552960
else:
2956-
x, y = self.artists[0].get_data()
2961+
x, y = self._selection_artist.get_data()
29572962
x0, x1 = min(x), max(x)
29582963
y0, y1 = min(y), max(y)
29592964
return x0, y0, x1 - x0, y1 - y0
@@ -3017,13 +3022,13 @@ def _draw_shape(self, extents):
30173022
ymax = min(ymax, ylim[1])
30183023

30193024
if self._drawtype == 'box':
3020-
self.artists[0].set_x(xmin)
3021-
self.artists[0].set_y(ymin)
3022-
self.artists[0].set_width(xmax - xmin)
3023-
self.artists[0].set_height(ymax - ymin)
3025+
self._selection_artist.set_x(xmin)
3026+
self._selection_artist.set_y(ymin)
3027+
self._selection_artist.set_width(xmax - xmin)
3028+
self._selection_artist.set_height(ymax - ymin)
30243029

30253030
elif self._drawtype == 'line':
3026-
self.artists[0].set_data([xmin, xmax], [ymin, ymax])
3031+
self._selection_artist.set_data([xmin, xmax], [ymin, ymax])
30273032

30283033
def _set_active_handle(self, event):
30293034
"""Set active handle based on the location of the mouse event."""
@@ -3067,7 +3072,7 @@ def _set_active_handle(self, event):
30673072

30683073
def _contains(self, event):
30693074
"""Return True if event is within the patch."""
3070-
return self.artists[0].contains(event, radius=0)[0]
3075+
return self._selection_artist.contains(event, radius=0)[0]
30713076

30723077
@property
30733078
def geometry(self):
@@ -3078,12 +3083,12 @@ def geometry(self):
30783083
of the four corners of the rectangle starting and ending
30793084
in the top left corner.
30803085
"""
3081-
if hasattr(self.artists[0], 'get_verts'):
3086+
if hasattr(self._selection_artist, 'get_verts'):
30823087
xfm = self.ax.transData.inverted()
3083-
y, x = xfm.transform(self.artists[0].get_verts()).T
3088+
y, x = xfm.transform(self._selection_artist.get_verts()).T
30843089
return np.array([x, y])
30853090
else:
3086-
return np.array(self.artists[0].get_data())
3091+
return np.array(self._selection_artist.get_data())
30873092

30883093

30893094
@docstring.Substitution(_RECTANGLESELECTOR_PARAMETERS_DOCSTRING.replace(
@@ -3137,24 +3142,24 @@ def _draw_shape(self, extents):
31373142
b = (ymax - ymin) / 2.
31383143

31393144
if self._drawtype == 'box':
3140-
self.artists[0].center = center
3141-
self.artists[0].width = 2 * a
3142-
self.artists[0].height = 2 * b
3145+
self._selection_artist.center = center
3146+
self._selection_artist.width = 2 * a
3147+
self._selection_artist.height = 2 * b
31433148
else:
31443149
rad = np.deg2rad(np.arange(31) * 12)
31453150
x = a * np.cos(rad) + center[0]
31463151
y = b * np.sin(rad) + center[1]
3147-
self.artists[0].set_data(x, y)
3152+
self._selection_artist.set_data(x, y)
31483153

31493154
@property
31503155
def _rect_bbox(self):
31513156
if self._drawtype == 'box':
3152-
x, y = self.artists[0].center
3153-
width = self.artists[0].width
3154-
height = self.artists[0].height
3157+
x, y = self._selection_artist.center
3158+
width = self._selection_artist.width
3159+
height = self._selection_artist.height
31553160
return x - width / 2., y - height / 2., width, height
31563161
else:
3157-
x, y = self.artists[0].get_data()
3162+
x, y = self._selection_artist.get_data()
31583163
x0, x1 = min(x), max(x)
31593164
y0, y1 = min(y), max(y)
31603165
return x0, y0, x1 - x0, y1 - y0
@@ -3218,7 +3223,7 @@ def onpress(self, event):
32183223

32193224
def _press(self, event):
32203225
self.verts = [self._get_data(event)]
3221-
self.artists[0].set_visible(True)
3226+
self._selection_artist.set_visible(True)
32223227

32233228
def onrelease(self, event):
32243229
self.release(event)
@@ -3227,16 +3232,16 @@ def _release(self, event):
32273232
if self.verts is not None:
32283233
self.verts.append(self._get_data(event))
32293234
self.onselect(self.verts)
3230-
self.artists[0].set_data([[], []])
3231-
self.artists[0].set_visible(False)
3235+
self._selection_artist.set_data([[], []])
3236+
self._selection_artist.set_visible(False)
32323237
self.verts = None
32333238

32343239
def _onmove(self, event):
32353240
if self.verts is None:
32363241
return
32373242
self.verts.append(self._get_data(event))
32383243

3239-
self.artists[0].set_data(list(zip(*self.verts)))
3244+
self._selection_artist.set_data(list(zip(*self.verts)))
32403245

32413246
self.update()
32423247

@@ -3343,7 +3348,7 @@ def __init__(self, ax, onselect, useblit=False,
33433348
self.set_visible(True)
33443349

33453350
line = _api.deprecated("3.5")(
3346-
property(lambda self: self.artists[0])
3351+
property(lambda self: self._selection_artist)
33473352
)
33483353

33493354
vertex_select_radius = _api.deprecated("3.5", name="vertex_select_radius",
@@ -3459,7 +3464,7 @@ def _onmove(self, event):
34593464
# Position pending vertex.
34603465
else:
34613466
# Calculate distance to the start vertex.
3462-
x0, y0 = self.artists[0].get_transform().transform((self._xs[0],
3467+
x0, y0 = self._selection_artist.get_transform().transform((self._xs[0],
34633468
self._ys[0]))
34643469
v0_dist = np.hypot(x0 - event.x, y0 - event.y)
34653470
# Lock on to the start vertex if near it and ready to complete.
@@ -3500,7 +3505,7 @@ def _on_key_release(self, event):
35003505

35013506
def _draw_polygon(self):
35023507
"""Redraw the polygon based on the new vertex positions."""
3503-
self.artists[0].set_data(self._xs, self._ys)
3508+
self._selection_artist.set_data(self._xs, self._ys)
35043509
# Only show one tool handle at the start and end vertex of the polygon
35053510
# if the polygon is completed or the user is locked on to the start
35063511
# vertex.

0 commit comments

Comments
 (0)