From fc2b8178bb579a13a4dc935903d24e3e054e4e9c Mon Sep 17 00:00:00 2001 From: Tim Hoffmann <2836374+timhoffm@users.noreply.github.com> Date: Mon, 10 Jul 2023 23:09:37 +0200 Subject: [PATCH] Improve exception message for set_ticks() kwargs without labels Followup to #24334 Closes #26283. --- lib/matplotlib/axis.py | 11 +++++++---- lib/matplotlib/tests/test_axes.py | 2 +- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/lib/matplotlib/axis.py b/lib/matplotlib/axis.py index 3e0b5c926208..96680fc862f4 100644 --- a/lib/matplotlib/axis.py +++ b/lib/matplotlib/axis.py @@ -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 ----- @@ -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 to set_ticks()") result = self._set_tick_locations(ticks, minor=minor) if labels is not None: self.set_ticklabels(labels, minor=minor, **kwargs) diff --git a/lib/matplotlib/tests/test_axes.py b/lib/matplotlib/tests/test_axes.py index 8b24f348ca03..947c706e900f 100644 --- a/lib/matplotlib/tests/test_axes.py +++ b/lib/matplotlib/tests/test_axes.py @@ -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)