Skip to content

Commit 07f7a83

Browse files
committed
Warn on redundant definition of plot properties
`plt.plot(x, y, fmt)` allows to specify marker, linestyle and color via `fmt`. Warn if there are additionally keyword arguments that specify the same properties.
1 parent 4e4410d commit 07f7a83

File tree

2 files changed

+18
-3
lines changed

2 files changed

+18
-3
lines changed

lib/matplotlib/axes/_base.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -417,10 +417,16 @@ def _plot_args(self, tup, kwargs, return_kwargs=False):
417417
raise ValueError("x, y, and format string must not be None")
418418

419419
kw = {}
420-
for k, v in zip(('linestyle', 'marker', 'color'),
421-
(linestyle, marker, color)):
420+
for prop_name, v in zip(('linestyle', 'marker', 'color'),
421+
(linestyle, marker, color)):
422422
if v is not None:
423-
kw[k] = v
423+
if prop_name in kwargs:
424+
_api.warn_external(
425+
f"'{prop_name}' is redundantly defined via fmt string "
426+
"and keyword argument. The keyword argument will "
427+
"take precedence."
428+
)
429+
kw[prop_name] = v
424430

425431
if len(tup) == 2:
426432
x = _check_1d(tup[0])

lib/matplotlib/tests/test_axes.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -624,6 +624,15 @@ def test_fill_units():
624624
fig.autofmt_xdate()
625625

626626

627+
def test_plot_format_kwarg_redundant():
628+
with pytest.warns(UserWarning, match="marker .* redundantly defined"):
629+
plt.plot([0], [0], 'o', marker='x')
630+
with pytest.warns(UserWarning, match="linestyle .* redundantly defined"):
631+
plt.plot([0], [0], '-', linestyle='--')
632+
with pytest.warns(UserWarning, match="color .* redundantly defined"):
633+
plt.plot([0], [0], 'r', color='blue')
634+
635+
627636
@image_comparison(['single_point', 'single_point'])
628637
def test_single_point():
629638
# Issue #1796: don't let lines.marker affect the grid

0 commit comments

Comments
 (0)