Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit dd2e6f8

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

File tree

2 files changed

+35
-9
lines changed

2 files changed

+35
-9
lines changed

lib/matplotlib/axis.py

Lines changed: 20 additions & 9 deletions
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,23 @@ 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 a fixed number of "
1987+
"ticks, i.e. after set_ticks() or using 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(
1992+
"ignore",
1993+
message="FixedFormatter should only be used together with FixedLocator")
1994+
if minor:
1995+
self.set_minor_formatter(formatter)
1996+
locs = self.get_minorticklocs()
1997+
ticks = self.get_minor_ticks(len(locs))
1998+
else:
1999+
self.set_major_formatter(formatter)
2000+
locs = self.get_majorticklocs()
2001+
ticks = self.get_major_ticks(len(locs))
19912002

19922003
ret = []
19932004
if fontdict is not None:

lib/matplotlib/tests/test_axes.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6063,6 +6063,21 @@ 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(
6070+
UserWarning,
6071+
match=r'set_ticklabels\(\) should only be used with a fixed number'):
6072+
ax.set_xticklabels(['0', '0.1'])
6073+
# note that the axis is still using a FixedLocator:
6074+
fig, ax = plt.subplots()
6075+
ax.set_xticks([0, 0.5, 1])
6076+
with pytest.raises(ValueError,
6077+
match='The number of FixedLocator locations'):
6078+
ax.set_xticklabels(['0', '0.1'])
6079+
6080+
60666081
def test_tick_label_update():
60676082
# test issue 9397
60686083

0 commit comments

Comments
 (0)