Skip to content

Trying to set the labels without setting ticks through pyplot now raises TypeError* #15927

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions doc/api/next_api_changes/behaviour.rst
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,12 @@ did nothing, when passed an unsupported value. It now raises a ``ValueError``.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
``backend_pgf.LatexManager.latex`` is now created with ``encoding="utf-8"``, so
its ``stdin``, ``stdout``, and ``stderr`` attributes are utf8-encoded.

``pyplot.xticks()`` and ``pyplot.yticks()``
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Previously, passing labels without passing the ticks to either `.pyplot.xticks`
and `.pyplot.yticks` would result in

TypeError: object of type 'NoneType' has no len()

It now raises a ``TypeError`` with a proper description of the error.
22 changes: 14 additions & 8 deletions lib/matplotlib/pyplot.py
Original file line number Diff line number Diff line change
Expand Up @@ -1394,14 +1394,17 @@ def xticks(ticks=None, labels=None, **kwargs):
"""
ax = gca()

if ticks is None and labels is None:
if ticks is None:
locs = ax.get_xticks()
labels = ax.get_xticklabels()
elif labels is None:
if labels is not None:
raise TypeError("xticks(): Parameter 'labels' can't be set "
"without setting 'ticks'")
else:
locs = ax.set_xticks(ticks)

if labels is None:
labels = ax.get_xticklabels()
else:
locs = ax.set_xticks(ticks)
labels = ax.set_xticklabels(labels, **kwargs)
for l in labels:
l.update(kwargs)
Expand Down Expand Up @@ -1451,14 +1454,17 @@ def yticks(ticks=None, labels=None, **kwargs):
"""
ax = gca()

if ticks is None and labels is None:
if ticks is None:
locs = ax.get_yticks()
labels = ax.get_yticklabels()
elif labels is None:
if labels is not None:
raise TypeError("yticks(): Parameter 'labels' can't be set "
"without setting 'ticks'")
else:
locs = ax.set_yticks(ticks)

if labels is None:
labels = ax.get_yticklabels()
else:
locs = ax.set_yticks(ticks)
labels = ax.set_yticklabels(labels, **kwargs)
for l in labels:
l.update(kwargs)
Expand Down