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

Commitdf3d2ab

Browse files
committed
Improve argument checking for set_xticks().
1 parentfbfa28d commitdf3d2ab

File tree

4 files changed

+17
-8
lines changed

4 files changed

+17
-8
lines changed

‎lib/matplotlib/_api/__init__.py‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,8 @@ def check_shape(_shape, **kwargs):
162162
ifnisnotNone
163163
elsenext(dim_labels)
164164
fornintarget_shape))
165+
iflen(target_shape)==1:
166+
text_shape+=","
165167

166168
raiseValueError(
167169
f"{k!r} must be{len(target_shape)}D "

‎lib/matplotlib/axis.py‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2046,6 +2046,7 @@ def _set_tick_locations(self, ticks, *, minor=False):
20462046

20472047
# XXX if the user changes units, the information will be lost here
20482048
ticks=self.convert_units(ticks)
2049+
locator=mticker.FixedLocator(ticks)# validate ticks early.
20492050
forname,axisinself.axes._axis_map.items():
20502051
ifselfisaxis:
20512052
shared= [
@@ -2061,10 +2062,10 @@ def _set_tick_locations(self, ticks, *, minor=False):
20612062
axis.set_view_interval(min(ticks),max(ticks))
20622063
self.axes.stale=True
20632064
ifminor:
2064-
self.set_minor_locator(mticker.FixedLocator(ticks))
2065+
self.set_minor_locator(locator)
20652066
returnself.get_minor_ticks(len(ticks))
20662067
else:
2067-
self.set_major_locator(mticker.FixedLocator(ticks))
2068+
self.set_major_locator(locator)
20682069
returnself.get_major_ticks(len(ticks))
20692070

20702071
defset_ticks(self,ticks,labels=None,*,minor=False,**kwargs):

‎lib/matplotlib/tests/test_axes.py‎

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5788,13 +5788,18 @@ def test_set_ticks_with_labels(fig_test, fig_ref):
57885788
ax.set_yticks([2,4], ['A','B'],minor=True)
57895789

57905790

5791-
deftest_set_noniterable_ticklabels():
5792-
# Ensure a useful TypeError message is raised
5793-
# when given a non-iterable ticklabels argument
5794-
# Pull request #22710
5791+
deftest_xticks_bad_args():
5792+
ax=plt.figure().add_subplot()
57955793
withpytest.raises(TypeError,match='must be a sequence'):
5796-
fig,ax=plt.subplots(2)
5797-
ax[1].set_xticks([2,9],3.1)
5794+
ax.set_xticks([2,9],3.1)
5795+
withpytest.raises(ValueError,match='must be 1D'):
5796+
plt.xticks(np.arange(4).reshape((-1,1)))
5797+
withpytest.raises(ValueError,match='must be 1D'):
5798+
plt.xticks(np.arange(4).reshape((1,-1)))
5799+
withpytest.raises(ValueError,match='must be 1D'):
5800+
plt.xticks(np.arange(4).reshape((-1,1)),labels=range(4))
5801+
withpytest.raises(ValueError,match='must be 1D'):
5802+
plt.xticks(np.arange(4).reshape((1,-1)),labels=range(4))
57985803

57995804

58005805
deftest_subsampled_ticklabels():

‎lib/matplotlib/ticker.py‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1718,6 +1718,7 @@ class FixedLocator(Locator):
17181718

17191719
def__init__(self,locs,nbins=None):
17201720
self.locs=np.asarray(locs)
1721+
_api.check_shape((None,),locs=self.locs)
17211722
self.nbins=max(nbins,2)ifnbinsisnotNoneelseNone
17221723

17231724
defset_params(self,nbins=None):

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp