Skip to content

Warn on redundant definition of plot properties #19277

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 13, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion examples/misc/svg_filter_line.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
l1, = ax.plot([0.1, 0.5, 0.9], [0.1, 0.9, 0.5], "bo-",
mec="b", lw=5, ms=10, label="Line 1")
l2, = ax.plot([0.1, 0.5, 0.9], [0.5, 0.2, 0.7], "rs-",
mec="r", lw=5, ms=10, color="r", label="Line 2")
mec="r", lw=5, ms=10, label="Line 2")


for l in [l1, l2]:
Expand Down
26 changes: 24 additions & 2 deletions lib/matplotlib/axes/_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,9 @@ def __call__(self, ax, renderer):
self._transform - ax.figure.transSubfigure)


_FORMAT_UNSET = 'None'
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should that not have been an object() placeholder, to avoid confusion with other meanings of the string "none"?

Copy link
Member Author

@timhoffm timhoffm Jan 14, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@anntzer When linestyle or marker are not specified in a format string, they are deactivated. "None" actually serves as value down the road in set_linestyle(). We cannot have a proper sentinel (or would have to convert it again down the road).

My intention was to make it more clear what the semantics of the value are. Otherwise

and val != _FORMAT_UNSET):

would be and val != 'None'. Which is less understandable, and freaks me out because of the case-insensitivity of our 'none' string handling. I was about to write and val.lower() != 'none' since I didn't follow closely where that was coming from, but then again, val can be a 4-tuple for colors, which you cannot lower().

But thinking again, it's better to be explicit with 'none' and not pretend to have a sentinel, that actually has hidden meaning. See #19291.

Edit: It's not that "simple" 🙄 . #19291 fails a number of tests.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks for taking care of that.



def _process_plot_format(fmt):
"""
Convert a MATLAB style color/line style format string to a (*linestyle*,
Expand All @@ -129,6 +132,10 @@ def _process_plot_format(fmt):
* 'r--': red dashed lines
* 'C2--': the third color in the color cycle, dashed lines

The format is absolute in the sense that if a linestyle or marker is not
defined in *fmt*, there is no line or marker. This is expressed by
returning _FORMAT_UNSET for the respective quantity.

See Also
--------
matplotlib.Line2D.lineStyles, matplotlib.colors.cnames
Expand Down Expand Up @@ -196,9 +203,9 @@ def _process_plot_format(fmt):
if linestyle is None and marker is None:
linestyle = mpl.rcParams['lines.linestyle']
if linestyle is None:
linestyle = 'None'
linestyle = _FORMAT_UNSET
if marker is None:
marker = 'None'
marker = _FORMAT_UNSET

return linestyle, marker, color

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

if len(xy) == 2:
Expand Down
11 changes: 11 additions & 0 deletions lib/matplotlib/tests/test_axes.py
Original file line number Diff line number Diff line change
Expand Up @@ -624,6 +624,17 @@ def test_fill_units():
fig.autofmt_xdate()


def test_plot_format_kwarg_redundant():
with pytest.warns(UserWarning, match="marker .* redundantly defined"):
plt.plot([0], [0], 'o', marker='x')
with pytest.warns(UserWarning, match="linestyle .* redundantly defined"):
plt.plot([0], [0], '-', linestyle='--')
with pytest.warns(UserWarning, match="color .* redundantly defined"):
plt.plot([0], [0], 'r', color='blue')
# smoke-test: should not warn
plt.errorbar([0], [0], fmt='none', color='blue')


@image_comparison(['single_point', 'single_point'])
def test_single_point():
# Issue #1796: don't let lines.marker affect the grid
Expand Down