diff --git a/lib/matplotlib/axis.py b/lib/matplotlib/axis.py index cfab4ce19c40..f970a4452660 100644 --- a/lib/matplotlib/axis.py +++ b/lib/matplotlib/axis.py @@ -1708,7 +1708,9 @@ def set_ticklabels(self, ticklabels, *, minor=False, **kwargs): locator = (self.get_minor_locator() if minor else self.get_major_locator()) if isinstance(locator, mticker.FixedLocator): - if len(locator.locs) != len(ticklabels): + # Passing [] as a list of ticklabels is often used as a way to + # remove all tick labels, so only error for > 0 ticklabels + if len(locator.locs) != len(ticklabels) and len(ticklabels) != 0: raise ValueError( "The number of FixedLocator locations" f" ({len(locator.locs)}), usually from a call to" diff --git a/lib/matplotlib/tests/test_axes.py b/lib/matplotlib/tests/test_axes.py index ed941744754e..33a4fe2914f9 100644 --- a/lib/matplotlib/tests/test_axes.py +++ b/lib/matplotlib/tests/test_axes.py @@ -4593,6 +4593,14 @@ def test_mismatched_ticklabels(): ax.xaxis.set_ticklabels(['a', 'b', 'c']) +def test_empty_ticks_fixed_loc(): + # Smoke test that [] can be used to unset all tick labels + fig, ax = plt.subplots() + ax.bar([1, 2], [1, 2]) + ax.set_xticks([1, 2]) + ax.set_xticklabels([]) + + @image_comparison(['retain_tick_visibility.png']) def test_retain_tick_visibility(): fig, ax = plt.subplots()