Skip to content

Commit 1c6ccce

Browse files
committed
Deprecate unused canvas parameter to MultiCursor
Closes matplotlib#21496.
1 parent db83eff commit 1c6ccce

File tree

4 files changed

+48
-11
lines changed

4 files changed

+48
-11
lines changed
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
The *canvas* parameter to ``MultiCursor``
2+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
3+
4+
... is deprecated. It has been unused for a while already.
5+
6+
Please remove the parameter and change the call from
7+
``MultiCursor(canvas, axes)`` to ``MultiCursor(axes)``. Both calls are
8+
valid throughout the deprecation period.

galleries/examples/widgets/multicursor.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
fig, ax3 = plt.subplots()
2727
ax3.plot(t, s3)
2828

29-
multi = MultiCursor(None, (ax1, ax2, ax3), color='r', lw=1)
29+
multi = MultiCursor((ax1, ax2, ax3), color='r', lw=1)
3030
plt.show()
3131

3232
# %%

lib/matplotlib/tests/test_widgets.py

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import operator
44
from unittest import mock
55

6+
import matplotlib as mpl
67
from matplotlib.backend_bases import DrawEvent, KeyEvent, MouseEvent
78
import matplotlib.colors as mcolors
89
import matplotlib.widgets as widgets
@@ -1680,15 +1681,22 @@ def test_polygon_selector_clear_method(ax):
16801681

16811682
@pytest.mark.parametrize("horizOn", [False, True])
16821683
@pytest.mark.parametrize("vertOn", [False, True])
1683-
def test_MultiCursor(horizOn, vertOn):
1684+
@pytest.mark.parametrize("with_deprecated_canvas", [False, True])
1685+
def test_MultiCursor(horizOn, vertOn, with_deprecated_canvas):
16841686
fig = plt.figure()
16851687
(ax1, ax3) = fig.subplots(2, sharex=True)
16861688
ax2 = plt.figure().subplots()
16871689

1688-
# useblit=false to avoid having to draw the figure to cache the renderer
1689-
multi = widgets.MultiCursor(
1690-
None, (ax1, ax2), useblit=False, horizOn=horizOn, vertOn=vertOn
1691-
)
1690+
if with_deprecated_canvas:
1691+
with pytest.warns(mpl.MatplotlibDeprecationWarning, match=r"canvas.*deprecat"):
1692+
multi = widgets.MultiCursor(
1693+
None, (ax1, ax2), useblit=False, horizOn=horizOn, vertOn=vertOn
1694+
)
1695+
else:
1696+
# useblit=false to avoid having to draw the figure to cache the renderer
1697+
multi = widgets.MultiCursor(
1698+
(ax1, ax2), useblit=False, horizOn=horizOn, vertOn=vertOn
1699+
)
16921700

16931701
# Only two of the axes should have a line drawn on them.
16941702
assert len(multi.vlines) == 2

lib/matplotlib/widgets.py

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1989,12 +1989,19 @@ class MultiCursor(Widget):
19891989
Provide a vertical (default) and/or horizontal line cursor shared between
19901990
multiple Axes.
19911991
1992+
Call signatures::
1993+
1994+
MultiCursor(axes, *, ...)
1995+
MultiCursor(canvas, axes, *, ...) # deprecated
1996+
19921997
For the cursor to remain responsive you must keep a reference to it.
19931998
19941999
Parameters
19952000
----------
19962001
canvas : object
1997-
This parameter is entirely unused and only kept for back-compatibility.
2002+
This parameter is entirely unused.
2003+
2004+
.. deprecated:: 3.11
19982005
19992006
axes : list of `~matplotlib.axes.Axes`
20002007
The `~.axes.Axes` to attach the cursor to.
@@ -2021,11 +2028,25 @@ class MultiCursor(Widget):
20212028
See :doc:`/gallery/widgets/multicursor`.
20222029
"""
20232030

2024-
def __init__(self, canvas, axes, *, useblit=True, horizOn=False, vertOn=True,
2031+
def __init__(self, *args, useblit=True, horizOn=False, vertOn=True,
20252032
**lineprops):
2026-
# canvas is stored only to provide the deprecated .canvas attribute;
2027-
# once it goes away the unused argument won't need to be stored at all.
2028-
self._canvas = canvas
2033+
# Deprecation of canvas as the first attribute. When the deprecation exprires:
2034+
# - change the signature to __init__(self, axes, *, ...)
2035+
# - delete the "Call signatures" block in the docstring
2036+
# - delete this block
2037+
kwargs = {k: lineprops.pop(k)
2038+
for k in list(lineprops) if k in ("canvas", "axes")}
2039+
params = _api.select_matching_signature(
2040+
[lambda axes: locals(), lambda canvas, axes: locals()], *args, **kwargs)
2041+
if "canvas" in params:
2042+
_api.warn_deprecated(
2043+
"3.11",
2044+
message="The canvas parameter in MultiCursor is unused and deprecated "
2045+
"since %(since)s. Please remove it and call MultiCursor(axes) "
2046+
"instead of MultiCursor(canvas, axes). The latter will start raising "
2047+
"an error in %(removal)s"
2048+
)
2049+
axes = params["axes"]
20292050

20302051
self.axes = axes
20312052
self.horizOn = horizOn

0 commit comments

Comments
 (0)