8000 Deprecate visible attribute of selectors in favour of get_visible/set… · matplotlib/matplotlib@bddb7f7 · GitHub
[go: up one dir, main page]

Skip to content

Commit bddb7f7

Browse files
committed
Deprecate visible attribute of selectors in favour of get_visible/set_visible
1 parent 742a6e3 commit bddb7f7

File tree

3 files changed

+40
-17
lines changed

3 files changed

+40
-17
lines changed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Selector widget state internals
2+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
3+
The *visible* attribute has been deprecated, use *set_visible* or
4+
*get_visible* instead.

lib/matplotlib/tests/test_widgets.py

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,19 @@ def test_rectangle_selector():
6464
check_rectangle(props=dict(fill=True))
6565

6666

67+
def test_deprecation_selector_visible_attribute():
68+
ax = get_ax()
69+
tool = widgets.RectangleSelector(ax, lambda *args: None)
70+
71+
assert tool.get_visible()
72+
73+
with pytest.warns(
74+
MatplotlibDeprecationWarning,
75+
match="The visible attribute was deprecated"):
76+
tool.visible = False
77+
assert not tool.get_visible()
78+
79+
6780
@pytest.mark.parametrize('drag_from_anywhere, new_center',
6881
[[True, (60, 75)],
6982
[False, (30, 20)]])
@@ -749,18 +762,18 @@ def test_selector_clear_method(ax, selector):
749762
tool = widgets.RectangleSelector(ax, onselect=noop, interactive=True)
750763
click_and_drag(tool, start=(10, 10), end=(100, 120))
751764
assert tool._selection_completed
752-
assert tool.visible
765+
assert tool.get_visible()
753766
if selector == 'span':
754767
assert tool.extents == (10, 100)
755768

756769
tool.clear()
757770
assert not tool._selection_completed
758-
assert not tool.visible
771+
assert not tool.get_visible()
759772

760773
# Do another cycle of events to make sure we can
761774
click_and_drag(tool, start=(10, 10), end=(50, 120))
762775
assert tool._selection_completed
763-
assert tool.visible
776+
assert tool.get_visible()
764777
if selector == 'span':
765778
assert tool.extents == (10, 50)
766779

lib/matplotlib/widgets.py

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1801,7 +1801,7 @@ def __init__(self, ax, onselect, useblit=False, button=None,
18011801
state_modifier_keys=None, use_data_coordinates=False):
18021802
super().__init__(ax)
18031803

1804-
self.visible = True
1804+
self._visible = True
18051805
self.onselect = onselect
18061806
self.useblit = useblit and self.canvas.supports_blit
18071807
self.connect_default_events()
@@ -2048,11 +2048,20 @@ def _on_key_release(self, event):
20482048
"""Key release event handler."""
20492049

20502050
def set_visible(self, visible):
2051-
"""Set the visibility of our artists."""
2052-
self.visible = visible
2051+
"""Set the visibility of the selector artists."""
2052+
self._visible = visible
20532053
for artist in self.artists:
20542054
artist.set_visible(visible)
20552055

2056+
def get_visible(self):
2057+
"""Get the visibility of the selector artists."""
2058+
return self._visible
2059+
2060+
visible = _api.deprecated("3.6", name="visible",
2061+
alternative="set_visible or get_visible")(
2062+
property(lambda self: self._visible,
2063+
lambda self, value: setattr(self, "_visible", value)))
2064+
20562065
def clear(self):
20572066
"""Clear the selection and set the selector ready to make a new one."""
20582067
self._selection_completed = False
@@ -2259,8 +2268,6 @@ def __init__(self, ax, onselect, direction, minspan=0, useblit=False,
22592268
props['animated'] = self.useblit
22602269

22612270
self.direction = direction
2262-
2263-
self.visible = True
22642271
self._extents_on_press = None
22652272

22662273
# self._pressv is deprecated and we don't use it internally anymore
@@ -2397,11 +2404,11 @@ def _press(self, event):
23972404
# when the press event outside the span, we initially set the
23982405
# visibility to False and extents to (v, v)
23992406
# update will be called when setting the extents
2400-
self.visible = False
2407+
self._visible = False
24012408
self.extents = v, v
24022409
# We need to set the visibility back, so the span selector will be
24032410
# drawn when necessary (span width > 0)
2404-
self.visible = True
2411+
self._visible = True
24052412
else:
24062413
self.set_visible(True)
24072414

@@ -2579,7 +2586,7 @@ def extents(self, extents):
25792586
if self._interactive:
25802587
# Update displayed handles
25812588
self._edge_handles.set_data(self.extents)
2582-
self.set_visible(self.visible)
2589+
self.set_visible(self._visible)
25832590
self.update()
25842591

25852592

@@ -2893,7 +2900,6 @@ def __init__(self, ax, onselect, drawtype='box',
28932900
state_modifier_keys=state_modifier_keys,
28942901
use_data_coordinates=use_data_coordinates)
28952902

2896-
self.visible = True
28972903
self._interactive = interactive
28982904
self.drag_from_anywhere = drag_from_anywhere
28992905
self.ignore_event_outside = ignore_event_outside
@@ -2912,14 +2918,14 @@ def __init__(self, ax, onselect, drawtype='box',
29122918
"%(removal)s."
29132919
"Use props=dict(visible=False) instead.")
29142920
drawtype = 'line'
2915-
self.visible = False
2921+
self._visible = False
29162922

29172923
if drawtype == 'box':
29182924
if props is None:
29192925
props = dict(facecolor='red', edgecolor='black',
29202926
alpha=0.2, fill=True)
29212927
props['animated'] = self.useblit
2922-
self.visible = props.pop('visible', self.visible)
2928+
self._visible = props.pop('visible', self._visible)
29232929
self._props = props
29242930
to_draw = self._init_shape(**self._props)
29252931
self.ax.add_patch(to_draw)
@@ -3016,9 +3022,9 @@ def _press(self, event):
30163022
self._allow_creation):
30173023
x = event.xdata
30183024
y = event.ydata
3019-
self.visible = False
3025+
self._visible = False
30203026
self.extents = x, x, y, y
3021-
self.visible = True
3027+
self._visible = True
30223028
else:
30233029
self.set_visible(True)
30243030

@@ -3312,7 +3318,7 @@ def extents(self, extents):
33123318
self._corner_handles.set_data(*self.corners)
33133319
self._edge_handles.set_data(*self.edge_centers)
33143320
self._center_handle.set_data(*self.center)
3315-
self.set_visible(self.visible)
3321+
self.set_visible(self._visible)
33163322
self.update()
33173323

33183324
@property

0 commit comments

Comments
 (0)
0