8000 Improve argument checking for set_xticks(). · matplotlib/matplotlib@df3d2ab · GitHub
[go: up one dir, main page]

Skip to content

Commit df3d2ab

Browse files
committed
Improve argument checking for set_xticks().
1 parent fbfa28d commit df3d2ab

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
if n is not None
163163
else next(dim_labels)
164164
for n in target_shape))
165+
if len(target_shape) == 1:
166+
text_shape += ","
165167

166168
raise ValueError(
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
for name, axis in self.axes._axis_map.items():
20502051
if self is axis:
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
if minor:
2064-
self.set_minor_locator(mticker.FixedLocator(ticks))
2065+
self.set_minor_locator(locator)
20652066
return self.get_minor_ticks(len(ticks))
20662067
else:
2067-
self.set_major_locator(mticker.FixedLocator(ticks))
2068+
self.set_major_locator(locator)
20682069
return self.get_major_ticks(len(ticks))
20692070

20702071
def set_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-
def test_set_noniterable_ticklabels():
5792-
# Ensure a useful TypeError message is raised
5793-
# when given a non-iterable ticklabels argument
5794-
# Pull request #22710
5791+
def test_xticks_bad_args():
5792+
ax = plt.figure().add_subplot()
57955793
with pytest.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+
with pytest.raises(ValueError, match='must be 1D'):
5796+
plt.xticks(np.arange(4).reshape((-1, 1)))
5797+
with pytest.raises(ValueError, match='must be 1D'):
5798+
plt.xticks(np.arange(4).reshape((1, -1)))
5799+
with pytest.raises(ValueError, match='must be 1D'):
5800+
plt.xticks(np.arange(4).reshape((-1, 1)), labels=range(4))
5801+
with pytest.raises(ValueError, match='must be 1D'):
5802+
plt.xticks(np.arange(4).reshape((1, -1)), labels=range(4))
57985803

57995804

58005805
def test_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) if nbins is not None else None
17221723

17231724
def set_params(self, nbins=None):

0 commit comments

Comments
 (0)
0