Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 62af56b

Browse files
committed
Deprecate mixing positional and keyword args for legend(handles, labels)
This was previously already a warning and also broken.
1 parent fcd5bb1 commit 62af56b

File tree

3 files changed

+14
-7
lines changed

3 files changed

+14
-7
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
Mixing positional and keyword arguments for ``legend`` handles and labels
2+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
3+
This was previously a plain warning, but is now deprecated. If passing
4+
*handles* and *labels*, they must be passed either both positionally or both as
5+
keyword.

lib/matplotlib/legend.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1301,7 +1301,7 @@ def _parse_legend_args(axs, *args, handles=None, labels=None, **kwargs):
13011301
legend(handles=handles, labels=labels)
13021302
13031303
The behavior for a mixture of positional and keyword handles and labels
1304-
is undefined and issues a warning.
1304+
is undefined and issues a warning; it will be an error in the future.
13051305
13061306
Parameters
13071307
----------
@@ -1334,8 +1334,10 @@ def _parse_legend_args(axs, *args, handles=None, labels=None, **kwargs):
13341334
handlers = kwargs.get('handler_map')
13351335

13361336
if (handles is not None or labels is not None) and args:
1337-
_api.warn_external("You have mixed positional and keyword arguments, "
1338-
"some input may be discarded.")
1337+
_api.warn_deprecated("3.9", message=(
1338+
"You have mixed positional and keyword arguments, some input may "
1339+
"be discarded. This is deprecated since %(since)s and will "
1340+
"become an error %(removal)s."))
13391341

13401342
if (hasattr(handles, "__len__") and
13411343
hasattr(labels, "__len__") and

lib/matplotlib/tests/test_legend.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -405,10 +405,10 @@ def test_warn_mixed_args_and_kwargs(self):
405405
th = np.linspace(0, 2*np.pi, 1024)
406406
lns, = ax.plot(th, np.sin(th), label='sin')
407407
lnc, = ax.plot(th, np.cos(th), label='cos')
408-
with pytest.warns(UserWarning) as record:
408+
with pytest.warns(DeprecationWarning) as record:
409409
ax.legend((lnc, lns), labels=('a', 'b'))
410410
assert len(record) == 1
411-
assert str(record[0].message) == (
411+
assert str(record[0].message).startswith(
412412
"You have mixed positional and keyword arguments, some input may "
413413
"be discarded.")
414414

@@ -474,10 +474,10 @@ def test_warn_args_kwargs(self):
474474
fig, axs = plt.subplots(1, 2)
475475
lines = axs[0].plot(range(10))
476476
lines2 = axs[1].plot(np.arange(10) * 2.)
477-
with pytest.warns(UserWarning) as record:
477+
with pytest.warns(DeprecationWarning) as record:
478478
fig.legend((lines, lines2), labels=('a', 'b'))
479479
assert len(record) == 1
480-
assert str(record[0].message) == (
480+
assert str(record[0].message).startswith(
481481
"You have mixed positional and keyword arguments, some input may "
482482
"be discarded.")
483483

0 commit comments

Comments
 (0)