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

Skip to content

Commit 86dd139

Browse files
committed
Trying to set the labels without setting ticks through pyplot now raises TypeError with a proper message
1 parent 9d00ca8 commit 86dd139

File tree

2 files changed

+23
-8
lines changed

2 files changed

+23
-8
lines changed

doc/api/next_api_changes/behaviour.rst

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,3 +41,12 @@ did nothing, when passed an unsupported value. It now raises a ``ValueError``.
4141
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
4242
``backend_pgf.LatexManager.latex`` is now created with ``encoding="utf-8"``, so
4343
its ``stdin``, ``stdout``, and ``stderr`` attributes are utf8-encoded.
44+
45+
``pyplot.xticks()`` and ``pyplot.yticks()``
46+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
47+
Previously, passing labels without passing the ticks to either `.pyplot.xticks`
48+
and `.pyplot.yticks` would result in
49+
50+
TypeError: object of type 'NoneType' has no len()
51+
52+
It now raises a ``TypeError`` with a proper description of the error.

lib/matplotlib/pyplot.py

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1394,14 +1394,17 @@ def xticks(ticks=None, labels=None, **kwargs):
13941394
"""
13951395
ax = gca()
13961396

1397-
if ticks is None and labels is None:
1397+
if ticks is None:
13981398
locs = ax.get_xticks()
1399-
labels = ax.get_xticklabels()
1400-
elif labels is None:
1399+
if labels is not None:
1400+
raise TypeError("xticks(): Parameter 'labels' can't be set "
1401+
"without setting 'ticks'")
1402+
else:
14011403
locs = ax.set_xticks(ticks)
1404+
1405+
if labels is None:
14021406
labels = ax.get_xticklabels()
14031407
else:
1404-
locs = ax.set_xticks(ticks)
14051408
labels = ax.set_xticklabels(labels, **kwargs)
14061409
for l in labels:
14071410
l.update(kwargs)
@@ -1451,14 +1454,17 @@ def yticks(ticks=None, labels=None, **kwargs):
14511454
"""
14521455
ax = gca()
14531456

1454-
if ticks is None and labels is None:
1457+
if ticks is None:
14551458
locs = ax.get_yticks()
1456-
labels = ax.get_yticklabels()
1457-
elif labels is None:
1459+
if labels is not None:
1460+
raise TypeError("yticks(): Parameter 'labels' can't be set "
1461+
"without setting 'ticks'")
1462+
else:
14581463
locs = ax.set_yticks(ticks)
1464+
1465+
if labels is None:
14591466
labels = ax.get_yticklabels()
14601467
else:
1461-
locs = ax.set_yticks(ticks)
14621468
labels = ax.set_yticklabels(labels, **kwargs)
14631469
for l in labels:
14641470
l.update(kwargs)

0 commit comments

Comments
 (0)