Skip to content

Improve exception message for set_ticks() kwargs without labels #26285

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
Jul 10, 2023
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
11 changes: 7 additions & 4 deletions lib/matplotlib/axis.py
Original file line number Diff line number Diff line change
Expand Up @@ -2055,8 +2055,8 @@ def set_ticks(self, ticks, labels=None, *, minor=False, **kwargs):
minor : bool, default: False
If ``False``, set the major ticks; if ``True``, the minor ticks.
**kwargs
`.Text` properties for the labels. These take effect only if you
pass *labels*. In other cases, please use `~.Axes.tick_params`.
`.Text` properties for the labels. Using these is only allowed if
you pass *labels*. In other cases, please use `~.Axes.tick_params`.

Notes
-----
Expand All @@ -2066,8 +2066,11 @@ def set_ticks(self, ticks, labels=None, *, minor=False, **kwargs):
ticks.
"""
if labels is None and kwargs:
raise ValueError('labels argument cannot be None when '
'kwargs are passed')
first_key = next(iter(kwargs))
raise ValueError(
f"Incorrect use of keyword argument {first_key!r}. Keyword arguments "
"other than 'minor' modify the text labels and can only be used if "
"'labels' are passed as well.")
result = self._set_tick_locations(ticks, minor=minor)
if labels is not None:
self.set_ticklabels(labels, minor=minor, **kwargs)
Expand Down
2 changes: 1 addition & 1 deletion lib/matplotlib/tests/test_axes.py
Original file line number Diff line number Diff line change
Expand Up @@ -5987,7 +5987,7 @@ def test_set_ticks_kwargs_raise_error_without_labels():
"""
fig, ax = plt.subplots()
ticks = [1, 2, 3]
with pytest.raises(ValueError):
with pytest.raises(ValueError, match="Incorrect use of keyword argument 'alpha'"):
ax.xaxis.set_ticks(ticks, alpha=0.5)


Expand Down