Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitdd2e6f8

Browse files
committed
WARN: fix warning for set_ticklabels
1 parent8c58e42 commitdd2e6f8

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
importfunctools
77
importlogging
88
fromnumbersimportReal
9+
importwarnings
910

1011
importnumpyasnp
1112

@@ -1965,7 +1966,10 @@ def set_ticklabels(self, labels, *, minor=False, fontdict=None, **kwargs):
19651966
raiseTypeError(f"{labels:=} must be a sequence")fromNone
19661967
locator= (self.get_minor_locator()ifminor
19671968
elseself.get_major_locator())
1968-
ifisinstance(locator,mticker.FixedLocator):
1969+
ifnotlabels:
1970+
# eg labels=[]:
1971+
formatter=mticker.NullFormatter()
1972+
elifisinstance(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
iflen(locator.locs)!=len(labels)andlen(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-
ifminor:
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+
withwarnings.catch_warnings():
1991+
warnings.filterwarnings(
1992+
"ignore",
1993+
message="FixedFormatter should only be used together with FixedLocator")
1994+
ifminor:
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
iffontdictisnotNone:

‎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+
deftest_warn_too_few_labels():
6067+
# note that the axis is still using an AutoLocator:
6068+
fig,ax=plt.subplots()
6069+
withpytest.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+
withpytest.raises(ValueError,
6077+
match='The number of FixedLocator locations'):
6078+
ax.set_xticklabels(['0','0.1'])
6079+
6080+
60666081
deftest_tick_label_update():
60676082
# test issue 9397
60686083

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp