Skip to content

Commit 0088182

Browse files
authored
Merge pull request matplotlib#30737 from timhoffm/multicursor
Deprecate unused canvas parameter to MultiCursor
2 parents 7b64c55 + d798256 commit 0088182

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
@@ -1689,15 +1690,22 @@ def test_polygon_selector_clear_method(ax):
16891690

16901691
@pytest.mark.parametrize("horizOn", [False, True])
16911692
@pytest.mark.parametrize("vertOn", [False, True])
1692-
def test_MultiCursor(horizOn, vertOn):
1693+
@pytest.mark.parametrize("with_deprecated_canvas", [False, True])
1694+
def test_MultiCursor(horizOn, vertOn, with_deprecated_canvas):
16931695
fig = plt.figure()
16941696
(ax1, ax3) = fig.subplots(2, sharex=True)
16951697
ax2 = plt.figure().subplots()
16961698

1697-
# useblit=false to avoid having to draw the figure to cache the renderer
1698-
multi = widgets.MultiCursor(
1699-
None, (ax1, ax2), useblit=False, horizOn=horizOn, vertOn=vertOn
1700-
)
1699+
if with_deprecated_canvas:
1700+
with pytest.warns(mpl.MatplotlibDeprecationWarning, match=r"canvas.*deprecat"):
1701+
multi = widgets.MultiCursor(
1702+
None, (ax1, ax2), useblit=False, horizOn=horizOn, vertOn=vertOn
1703+
)
1704+
else:
1705+
# useblit=false to avoid having to draw the figure to cache the renderer
1706+
multi = widgets.MultiCursor(
1707+
(ax1, ax2), useblit=False, horizOn=horizOn, vertOn=vertOn
1708+
)
17011709

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

lib/matplotlib/widgets.py

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2017,12 +2017,19 @@ class MultiCursor(Widget):
20172017
Provide a vertical (default) and/or horizontal line cursor shared between
20182018
multiple Axes.
20192019
2020+
Call signatures::
2021+
2022+
MultiCursor(axes, *, ...)
2023+
MultiCursor(canvas, axes, *, ...) # deprecated
2024+
20202025
For the cursor to remain responsive you must keep a reference to it.
20212026
20222027
Parameters
20232028
----------
20242029
canvas : object
2025-
This parameter is entirely unused and only kept for back-compatibility.
2030+
This parameter is entirely unused.
2031+
2032+
.. deprecated:: 3.11
20262033
20272034
axes : list of `~matplotlib.axes.Axes`
20282035
The `~.axes.Axes` to attach the cursor to.
@@ -2049,11 +2056,25 @@ class MultiCursor(Widget):
20492056
See :doc:`/gallery/widgets/multicursor`.
20502057
"""
20512058

2052-
def __init__(self, canvas, axes, *, useblit=True, horizOn=False, vertOn=True,
2059+
def __init__(self, *args, useblit=True, horizOn=False, vertOn=True,
20532060
**lineprops):
2054-
# canvas is stored only to provide the deprecated .canvas attribute;
2055-
# once it goes away the unused argument won't need to be stored at all.
2056-
self._canvas = canvas
2061+
# Deprecation of canvas as the first attribute. When the deprecation expires:
2062+
# - change the signature to __init__(self, axes, *, ...)
2063+
# - delete the "Call signatures" block in the docstring
2064+
# - delete this block
2065+
kwargs = {k: lineprops.pop(k)
2066+
for k in list(lineprops) if k in ("canvas", "axes")}
2067+
params = _api.select_matching_signature(
2068+
[lambda axes: locals(), lambda canvas, axes: locals()], *args, **kwargs)
2069+
if "canvas" in params:
2070+
_api.warn_deprecated(
2071+
"3.11",
2072+
message="The canvas parameter in MultiCursor is unused and deprecated "
2073+
"since %(since)s. Please remove it and call MultiCursor(axes) "
2074+
"instead of MultiCursor(canvas, axes). The latter will start raising "
2075+
"an error in %(removal)s"
2076+
)
2077+
axes = params["axes"]
20572078

20582079
self.axes = axes
20592080
self.horizOn = horizOn

0 commit comments

Comments
 (0)