Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 12f6a9e

Browse filesBrowse 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. Closes #19275.
1 parent acdfe3f commit 12f6a9e
Copy full SHA for 12f6a9e

File tree

Expand file treeCollapse file tree

2 files changed

+35
-2
lines changed
Filter options
Expand file treeCollapse file tree

2 files changed

+35
-2
lines changed

‎lib/matplotlib/axes/_base.py

Copy file name to clipboardExpand all lines: lib/matplotlib/axes/_base.py
+24-2Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,9 @@ def __call__(self, ax, renderer):
117117
self._transform - ax.figure.transSubfigure)
118118

119119

120+
_FORMAT_UNSET = 'None'
121+
122+
120123
def _process_plot_format(fmt):
121124
"""
122125
Convert a MATLAB style color/line style format string to a (*linestyle*,
@@ -129,6 +132,10 @@ def _process_plot_format(fmt):
129132
* 'r--': red dashed lines
130133
* 'C2--': the third color in the color cycle, dashed lines
131134
135+
The format is absolute in the sense that if a linestyle or marker is not
136+
defined in *fmt*, there is no line or marker. This is expressed by
137+
returning _FORMAT_UNSET for the respective quantity.
138+
132139
See Also
133140
--------
134141
matplotlib.Line2D.lineStyles, matplotlib.colors.cnames
@@ -196,9 +203,9 @@ def _process_plot_format(fmt):
196203
if linestyle is None and marker is None:
197204
linestyle = mpl.rcParams['lines.linestyle']
198205
if linestyle is None:
199-
linestyle = 'None'
206+
linestyle = _FORMAT_UNSET
200207
if marker is None:
201-
marker = 'None'
208+
marker = _FORMAT_UNSET
202209

203210
return linestyle, marker, color
204211

@@ -461,6 +468,21 @@ def _plot_args(self, tup, kwargs, return_kwargs=False):
461468
for prop_name, val in zip(('linestyle', 'marker', 'color'),
462469
(linestyle, marker, color)):
463470
if val is not None:
471+
# check for conflicts between fmt and kwargs
472+
if (fmt.lower() != 'none'
473+
and prop_name in kwargs
474+
and val != _FORMAT_UNSET):
475+
# Technically ``plot(x, y, 'o', ls='--')`` is a conflict
476+
# because 'o' implicitly unsets the linestyle
477+
# (linestyle=_FORMAT_UNSET).
478+
# We'll gracefully not warn in this case because an
479+
# explicit set via kwargs can be seen as intention to
480+
# override an implicit unset.
481+
_api.warn_external(
482+
f"{prop_name} is redundantly defined by the "
483+
f"'{prop_name}' keyword argument and the fmt string "
484+
f'"{fmt}" (-> {prop_name}={val!r}). The keyword '
485+
f"argument will take precedence.")
464486
kw[prop_name] = val
465487

466488
if len(xy) == 2:

‎lib/matplotlib/tests/test_axes.py

Copy file name to clipboardExpand all lines: lib/matplotlib/tests/test_axes.py
+11Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -624,6 +624,17 @@ 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+
# smoke-test: should not warn
635+
plt.errorbar([0], [0], fmt='none', color='blue')
636+
637+
627638
@image_comparison(['single_point', 'single_point'])
628639
def test_single_point():
629640
# Issue #1796: don't let lines.marker affect the grid

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.