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

Commitb0c11c4

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

File tree

2 files changed

+32
-9
lines changed

2 files changed

+32
-9
lines changed

‎lib/matplotlib/axis.py

Lines changed: 18 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,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-
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("ignore",message="FixedFormatter should only ")
1992+
ifminor:
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
iffontdictisnotNone:

‎lib/matplotlib/tests/test_axes.py

Lines changed: 14 additions & 0 deletions
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+
deftest_warn_too_few_labels():
6067+
# note that the axis is still using an AutoLocator:
6068+
fig,ax=plt.subplots()
6069+
withpytest.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+
withpytest.raises(ValueError,
6076+
match='The number of FixedLocator locations'):
6077+
ax.set_xticklabels(['0','0.1'])
6078+
6079+
60666080
deftest_tick_label_update():
60676081
# test issue 9397
60686082

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp