Skip to content

Commit

Permalink
Improve exception message for set_ticks() kwargs without labels
Browse files Browse the repository at this point in the history
  • Loading branch information
timhoffm committed Jul 10, 2023
1 parent a861b8a commit ae5da04
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 5 deletions.
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 passed 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

0 comments on commit ae5da04

Please sign in to comment.