Skip to content

Commit f650bf8

Browse files
authored
Merge pull request matplotlib#19934 from dstansby/drawtype
Deprecate drawtype to RectangleSelector
2 parents 3aad104 + 1ffb809 commit f650bf8

File tree

4 files changed

+59
-6
lines changed

4 files changed

+59
-6
lines changed
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
*drawtype* argument to RectangleSelector
2+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
3+
The *drawtype* keyword argument to
4+
`~matplotlib.widgets.RectangleSelector` is deprecated. In the future the
5+
behaviour will be the default behaviour of ``drawtype='box'``.
6+
7+
Support for ``drawtype=line`` will be removed altogether as it is not clear
8+
which points are within and outside a selector that is just a line.
9+
As a result, the ``lineprops`` keyword argument to
10+
`~matplotlib.widgets.RectangleSelector` is also deprecated.
11+
12+
To retain the behaviour of ``drawtype='none'``, use
13+
``rectprops={'visible': False}`` to make the drawn
14+
`~matplotlib.patches.Rectangle` invisible.
15+
16+
These changes also apply to `~matplotlib.widgets.EllipseSelector`, which
17+
is a sub-class of `~matplotlib.widgets.RectangleSelector`.

examples/widgets/rectangle_selector.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,8 @@ def toggle_selector(event):
4848
"Click and drag to draw a rectangle.\n"
4949
"Press 't' to toggle the selector on and off.")
5050

51-
# drawtype is 'box' or 'line' or 'none'
5251
toggle_selector.RS = RectangleSelector(ax, line_select_callback,
53-
drawtype='box', useblit=True,
52+
useblit=True,
5453
button=[1, 3], # disable middle button
5554
minspanx=5, minspany=5,
5655
spancoords='pixels',

lib/matplotlib/tests/test_widgets.py

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1+
from matplotlib._api.deprecation import MatplotlibDeprecationWarning
12
import matplotlib.colors as mcolors
23
import matplotlib.widgets as widgets
34
import matplotlib.pyplot as plt
4-
from matplotlib.testing.decorators import image_comparison
5+
from matplotlib.testing.decorators import check_figures_equal, image_comparison
56
from matplotlib.testing.widgets import do_event, get_ax, mock_event
67

78
from numpy.testing import assert_allclose
@@ -37,9 +38,19 @@ def onselect(epress, erelease):
3738

3839
def test_rectangle_selector():
3940
check_rectangle()
40-
check_rectangle(drawtype='line', useblit=False)
41+
42+
with pytest.warns(
43+
MatplotlibDeprecationWarning,
44+
match="Support for drawtype='line' is deprecated"):
45+
check_rectangle(drawtype='line', useblit=False)
46+
4147
check_rectangle(useblit=True, button=1)
42-
check_rectangle(drawtype='none', minspanx=10, minspany=10)
48+
49+
with pytest.warns(
50+
MatplotlibDeprecationWarning,
51+
match="Support for drawtype='none' is deprecated"):
52+
check_rectangle(drawtype='none', minspanx=10, minspany=10)
53+
4354
check_rectangle(minspanx=10, minspany=10, spancoords='pixels')
4455
check_rectangle(rectprops=dict(fill=True))
4556

@@ -500,3 +511,17 @@ def test_MultiCursor(horizOn, vertOn):
500511
assert l.get_xdata() == (.5, .5)
501512
for l in multi.hlines:
502513
assert l.get_ydata() == (.25, .25)
514+
515+
516+
@check_figures_equal()
517+
def test_rect_visibility(fig_test, fig_ref):
518+
# Check that requesting an invisible selector makes it invisible
519+
ax_test = fig_test.subplots()
520+
ax_ref = fig_ref.subplots()
521+
522+
def onselect(verts):
523+
pass
524+
525+
tool = widgets.RectangleSelector(ax_test, onselect,
526+
rectprops={'visible': False})
527+
tool.extents = (0.2, 0.8, 0.3, 0.7)

lib/matplotlib/widgets.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2185,6 +2185,8 @@ class RectangleSelector(_SelectorWidget):
21852185

21862186
_shape_klass = Rectangle
21872187

2188+
@_api.delete_parameter("3.5", "drawtype")
2189+
@_api.delete_parameter("3.5", "lineprops")
21882190
def __init__(self, ax, onselect, drawtype='box',
21892191
minspanx=0, minspany=0, useblit=False,
21902192
lineprops=None, rectprops=None, spancoords='data',
@@ -2270,6 +2272,11 @@ def onselect(eclick: MouseEvent, erelease: MouseEvent)
22702272
self.interactive = interactive
22712273

22722274
if drawtype == 'none': # draw a line but make it invisible
2275+
_api.warn_deprecated(
2276+
"3.5", message="Support for drawtype='none' is deprecated "
2277+
"since %(since)s and will be removed "
2278+
"%(removal)s."
2279+
"Use rectprops=dict(visible=False) instead.")
22732280
drawtype = 'line'
22742281
self.visible = False
22752282

@@ -2279,10 +2286,15 @@ def onselect(eclick: MouseEvent, erelease: MouseEvent)
22792286
alpha=0.2, fill=True)
22802287
rectprops['animated'] = self.useblit
22812288
self.rectprops = rectprops
2289+
self.visible = self.rectprops.pop('visible', self.visible)
22822290
self.to_draw = self._shape_klass((0, 0), 0, 1, visible=False,
22832291
**self.rectprops)
22842292
self.ax.add_patch(self.to_draw)
22852293
if drawtype == 'line':
2294+
_api.warn_deprecated(
2295+
"3.5", message="Support for drawtype='line' is deprecated "
2296+
"since %(since)s and will be removed "
2297+
"%(removal)s.")
22862298
if lineprops is None:
22872299
lineprops = dict(color='black', linestyle='-',
22882300
linewidth=2, alpha=0.5)
@@ -2609,7 +2621,7 @@ def toggle_selector(event):
26092621
fig, ax = plt.subplots()
26102622
ax.plot(x, y)
26112623
2612-
toggle_selector.ES = EllipseSelector(ax, onselect, drawtype='line')
2624+
toggle_selector.ES = EllipseSelector(ax, onselect)
26132625
fig.canvas.mpl_connect('key_press_event', toggle_selector)
26142626
plt.show()
26152627
"""

0 commit comments

Comments
 (0)