Skip to content

Commit c472067

Browse files
committed
WARN: fix warning for set_ticklabels
1 parent 8c58e42 commit c472067

File tree

2 files changed

+32
-9
lines changed

2 files changed

+32
-9
lines changed

lib/matplotlib/axis.py

+18-9
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import functools
77
import logging
88
from numbers import Real
9+
import warnings
910

1011
import numpy as np
1112

@@ -1965,7 +1966,10 @@ def set_ticklabels(self, labels, *, minor=False, fontdict=None, **kwargs):
19651966
raise TypeError(f"{labels:=} must be a sequence") from None
19661967
locator = (self.get_minor_locator() if minor
19671968
else self.get_major_locator())
1968-
if isinstance(locator, mticker.FixedLocator):
1969+
if not labels:
1970+
# eg labels=[]:
1971+
formatter = mticker.NullFormatter()
1972+
elif isinstance(locator, mticker.FixedLocator):
19691973
# Passing [] as a list of labels is often used as a way to
19701974
# remove all tick labels, so only error for > 0 labels
19711975
if len(locator.locs) != len(labels) and len(labels) != 0:
@@ -1978,16 +1982,21 @@ def set_ticklabels(self, labels, *, minor=False, fontdict=None, **kwargs):
19781982
func = functools.partial(self._format_with_dict, tickd)
19791983
formatter = mticker.FuncFormatter(func)
19801984
else:
1985+
_api.warn_external(
1986+
"set_ticklabels should only be used with set_ticks or "
1987+
"a FixedLocator.")
19811988
formatter = mticker.FixedFormatter(labels)
19821989

1983-
if minor:
1984-
self.set_minor_formatter(formatter)
1985-
locs = self.get_minorticklocs()
1986-
ticks = self.get_minor_ticks(len(locs))
1987-
else:
1988-
self.set_major_formatter(formatter)
1989-
locs = self.get_majorticklocs()
1990-
ticks = self.get_major_ticks(len(locs))
1990+
with warnings.catch_warnings():
1991+
warnings.filterwarnings("ignore", message="FixedFormatter should only ")
1992+
if minor:
1993+
self.set_minor_formatter(formatter)
1994+
locs = self.get_minorticklocs()
1995+
ticks = self.get_minor_ticks(len(locs))
1996+
else:
1997+
self.set_major_formatter(formatter)
1998+
locs = self.get_majorticklocs()
1999+
ticks = self.get_major_ticks(len(locs))
19912000

19922001
ret = []
19932002
if fontdict is not None:

lib/matplotlib/tests/test_axes.py

+14
Original file line numberDiff line numberDiff line change
@@ -6063,6 +6063,20 @@ def test_retain_tick_visibility():
60636063
ax.tick_params(axis="y", which="both", length=0)
60646064

60656065

6066+
def test_warn_too_few_labels():
6067+
# note that the axis is still using an AutoLocator:
6068+
fig, ax = plt.subplots()
6069+
with pytest.warns(UserWarning,
6070+
match='set_ticklabels should only be used with set_ticks or a'):
6071+
ax.set_xticklabels(['0', '0.1'])
6072+
# note that the axis is still using a FixedLocator:
6073+
fig, ax = plt.subplots()
6074+
ax.set_xticks([0, 0.5, 1])
6075+
with pytest.raises(ValueError,
6076+
match='The number of FixedLocator locations'):
6077+
ax.set_xticklabels(['0', '0.1'])
6078+
6079+
60666080
def test_tick_label_update():
60676081
# test issue 9397
60686082

0 commit comments

Comments
 (0)