Skip to content

Commit c9614de

Browse files
kjaintacaswell
kjain
andauthored
Fix the error- TypeError: 'float' object is not iterable (#22710)
Co-authored-by: Thomas A Caswell <tcaswell@gmail.com>
1 parent 6fedb48 commit c9614de

File tree

2 files changed

+14
-2
lines changed

2 files changed

+14
-2
lines changed

lib/matplotlib/axis.py

+5-2
Original file line numberDiff line numberDiff line change
@@ -1825,8 +1825,11 @@ def set_ticklabels(self, ticklabels, *, minor=False, **kwargs):
18251825
For each tick, includes ``tick.label1`` if it is visible, then
18261826
``tick.label2`` if it is visible, in that order.
18271827
"""
1828-
ticklabels = [t.get_text() if hasattr(t, 'get_text') else t
1829-
for t in ticklabels]
1828+
try:
1829+
ticklabels = [t.get_text() if hasattr(t, 'get_text') else t
1830+
for t in ticklabels]
1831+
except TypeError:
1832+
raise TypeError(f"{ticklabels:=} must be a sequence") from None
18301833
locator = (self.get_minor_locator() if minor
18311834
else self.get_major_locator())
18321835
if isinstance(locator, mticker.FixedLocator):

lib/matplotlib/tests/test_axes.py

+9
Original file line numberDiff line numberDiff line change
@@ -5390,6 +5390,15 @@ def test_set_ticks_with_labels(fig_test, fig_ref):
53905390
ax.set_yticks([2, 4], ['A', 'B'], minor=True)
53915391

53925392

5393+
def test_set_noniterable_ticklabels():
5394+
# Ensure a useful TypeError message is raised
5395+
# when given a non-iterable ticklabels argument
5396+
# Pull request #22710
5397+
with pytest.raises(TypeError, match='must be a sequence'):
5398+
fig, ax = plt.subplots(2)
5399+
ax[1].set_xticks([2, 9], 3.1)
5400+
5401+
53935402
def test_subsampled_ticklabels():
53945403
# test issue 11937
53955404
fig, ax = plt.subplots()

0 commit comments

Comments
 (0)