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

Skip to content

Commit 2219872

Browse files
authored
Merge pull request #19277 from timhoffm/warn-plot-fmt-and-kwarg
Warn on redundant definition of plot properties
2 parents 325fac9 + c000643 commit 2219872

File tree

3 files changed

+36
-3
lines changed

3 files changed

+36
-3
lines changed

examples/misc/svg_filter_line.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
l1, = ax.plot([0.1, 0.5, 0.9], [0.1, 0.9, 0.5], "bo-",
2121
mec="b", lw=5, ms=10, label="Line 1")
2222
l2, = ax.plot([0.1, 0.5, 0.9], [0.5, 0.2, 0.7], "rs-",
23-
mec="r", lw=5, ms=10, color="r", label="Line 2")
23+
mec="r", lw=5, ms=10, label="Line 2")
2424

2525

2626
for l in [l1, l2]:

lib/matplotlib/axes/_base.py

Lines 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

Lines 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)