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

Skip to content

Commit dbe6f35

Browse files
committed
Require a match between fixed ticks and ticklabels.
1 parent 6c71b67 commit dbe6f35

2 files changed

Lines changed: 15 additions & 2 deletions

File tree

lib/matplotlib/axis.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1635,6 +1635,11 @@ def set_ticklabels(self, ticklabels, *, minor=False, **kwargs):
16351635
locator = (self.get_minor_locator() if minor
16361636
else self.get_major_locator())
16371637
if isinstance(locator, mticker.FixedLocator):
1638+
if len(locator.locs) != len(ticklabels):
1639+
raise ValueError(
1640+
"The number of FixedLocator locations"
1641+
f" ({len(locator.locs)}) does not match"
1642+
f" the number of ticklabels ({len(ticklabels)}).")
16381643
tickd = {loc: lab for loc, lab in zip(locator.locs, ticklabels)}
16391644
func = functools.partial(self._format_with_dict, tickd)
16401645
formatter = mticker.FuncFormatter(func)

lib/matplotlib/tests/test_axes.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4441,8 +4441,8 @@ def test_set_get_ticklabels():
44414441
# set ticklabel to 1 plot in normal way
44424442
ax[0].set_xticks(range(10))
44434443
ax[0].set_yticks(range(10))
4444-
ax[0].set_xticklabels(['a', 'b', 'c', 'd'])
4445-
ax[0].set_yticklabels(['11', '12', '13', '14'])
4444+
ax[0].set_xticklabels(['a', 'b', 'c', 'd'] + 6 * [''])
4445+
ax[0].set_yticklabels(['11', '12', '13', '14'] + 6 * [''])
44464446

44474447
# set ticklabel to the other plot, expect the 2 plots have same label
44484448
# setting pass get_ticklabels return value as ticklabels argument
@@ -4464,6 +4464,14 @@ def test_subsampled_ticklabels():
44644464
assert labels == ['b', 'd', 'f', 'h', 'j']
44654465

44664466

4467+
def test_mismatched_ticklabels():
4468+
fig, ax = plt.subplots()
4469+
ax.plot(np.arange(10))
4470+
ax.xaxis.set_ticks([1.5, 2.5])
4471+
with pytest.raises(ValueError):
4472+
ax.xaxis.set_ticklabels(['a', 'b', 'c'])
4473+
4474+
44674475
@image_comparison(['retain_tick_visibility.png'])
44684476
def test_retain_tick_visibility():
44694477
fig, ax = plt.subplots()

0 commit comments

Comments
 (0)