Skip to content

Commit fc2b817

Browse files
committed
Improve exception message for set_ticks() kwargs without labels
Followup to #24334 Closes #26283.
1 parent a861b8a commit fc2b817

File tree

2 files changed

+8
-5
lines changed

2 files changed

+8
-5
lines changed

lib/matplotlib/axis.py

+7-4
Original file line numberDiff line numberDiff line change
@@ -2055,8 +2055,8 @@ def set_ticks(self, ticks, labels=None, *, minor=False, **kwargs):
20552055
minor : bool, default: False
20562056
If ``False``, set the major ticks; if ``True``, the minor ticks.
20572057
**kwargs
2058-
`.Text` properties for the labels. These take effect only if you
2059-
pass *labels*. In other cases, please use `~.Axes.tick_params`.
2058+
`.Text` properties for the labels. Using these is only allowed if
2059+
you pass *labels*. In other cases, please use `~.Axes.tick_params`.
20602060
20612061
Notes
20622062
-----
@@ -2066,8 +2066,11 @@ def set_ticks(self, ticks, labels=None, *, minor=False, **kwargs):
20662066
ticks.
20672067
"""
20682068
if labels is None and kwargs:
2069-
raise ValueError('labels argument cannot be None when '
2070-
'kwargs are passed')
2069+
first_key = next(iter(kwargs))
2070+
raise ValueError(
2071+
f"Incorrect use of keyword argument {first_key!r}. Keyword arguments "
2072+
"other than 'minor' modify the text labels and can only be passed if "
2073+
"'labels' are passed to set_ticks()")
20712074
result = self._set_tick_locations(ticks, minor=minor)
20722075
if labels is not None:
20732076
self.set_ticklabels(labels, minor=minor, **kwargs)

lib/matplotlib/tests/test_axes.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -5987,7 +5987,7 @@ def test_set_ticks_kwargs_raise_error_without_labels():
59875987
"""
59885988
fig, ax = plt.subplots()
59895989
ticks = [1, 2, 3]
5990-
with pytest.raises(ValueError):
5990+
with pytest.raises(ValueError, match="Incorrect use of keyword argument 'alpha'"):
59915991
ax.xaxis.set_ticks(ticks, alpha=0.5)
59925992

59935993

0 commit comments

Comments
 (0)