Skip to content

[Bug]: regression with setting ticklabels for colorbars in matplotlib 3.5.0b1 #20989

Closed
@neutrinoceros

Description

@neutrinoceros

Bug summary

matplotlib 3.5.0b1 breaks the following code, which runs fine on 3.4.3
The crash happens while running the last line:

cbar.ax.set_xticklabels(cbar.ax.get_xticklabels())

Admittedly this line seems like it should not do anything and certainly not produce an error.
In practice, my actual application contains the following:

cbar.ax.set_xticklabels(cbar.ax.get_xticklabels(), rotation=45)

which looks more like a hack than a supported usage of matplotlib's api, so I'd be happy to change it but I was not yet able to find a cleaner way to perform that rotation.
Still, no matter how untidy my application is, I'm fairly convinced that breaking it is a bug.

Code for reproduction

import matplotlib.pyplot as plt
import numpy as np

x, y = np.mgrid[1:100, 1:100]
z = np.random.random_sample(x.shape)

fig, ax = plt.subplots()

im = ax.contourf(x, y, z, levels=50)
cbar = fig.colorbar(im, orientation='horizontal')
cbar.ax.set_xticklabels(cbar.ax.get_xticklabels())

Actual outcome

Traceback
╭─────────────────────────────── Traceback (most recent call last) ────────────────────────────────╮
│ /private/tmp/dustyn/t_mpl.py:11 in <module>                                                      │
│                                                                                                  │
│    8                                                                                             │
│    9 im = ax.contourf(x, y, z, levels=50)                                                        │
│   10 cbar = fig.colorbar(im, orientation='horizontal')                                           │
│ ❱ 11 cbar.ax.set_xticklabels(cbar.ax.get_xticklabels())                                          │
│   12                                                                                             │
│                                                                                                  │
│ /Users/robcleme/.pyenv/versions/3.9.6/envs/mpl35_tmp/lib/python3.9/site-packages/matplotlib/axes │
│ /_base.py:75 in wrapper                                                                          │
│                                                                                                  │
│     72 │   │   get_method = attrgetter(f"{self.attr_name}.{self.method_name}")                   │
│     73 │   │                                                                                     │
│     74 │   │   def wrapper(self, *args, **kwargs):                                               │
│ ❱   75 │   │   │   return get_method(self)(*args, **kwargs)                                      │
│     76 │   │                                                                                     │
│     77 │   │   wrapper.__module__ = owner.__module__                                             │
│     78 │   │   wrapper.__name__ = name                                                           │
│                                                                                                  │
│ /Users/robcleme/.pyenv/versions/3.9.6/envs/mpl35_tmp/lib/python3.9/site-packages/matplotlib/axis │
│ .py:1798 in _set_ticklabels                                                                      │
│                                                                                                  │
│   1795 │   │   """                                                                               │
│   1796 │   │   if fontdict is not None:                                                          │
│   1797 │   │   │   kwargs.update(fontdict)                                                       │
│ ❱ 1798 │   │   return self.set_ticklabels(labels, minor=minor, **kwargs)                         │
│   1799 │                                                                                         │
│   1800 │   def _set_tick_locations(self, ticks, *, minor=False):                                 │
│   1801 │   │   # see docstring of set_ticks                                                      │
│                                                                                                  │
│ /Users/robcleme/.pyenv/versions/3.9.6/envs/mpl35_tmp/lib/python3.9/site-packages/matplotlib/axis │
│ .py:1720 in set_ticklabels                                                                       │
│                                                                                                  │
│   1717 │   │   │   # Passing [] as a list of ticklabels is often used as a way to                │
│   1718 │   │   │   # remove all tick labels, so only error for > 0 ticklabels                    │
│   1719 │   │   │   if len(locator.locs) != len(ticklabels) and len(ticklabels) != 0:             │
│ ❱ 1720 │   │   │   │   raise ValueError(                                                         │
│   1721 │   │   │   │   │   "The number of FixedLocator locations"                                │
│   1722 │   │   │   │   │   f" ({len(locator.locs)}), usually from a call to"                     │
│   1723 │   │   │   │   │   " set_ticks, does not match"                                          │
╰──────────────────────────────────────────────────────────────────────────────────────────────────╯
ValueError: The number of FixedLocator locations (51), usually from a call to set_ticks, does not match the number of ticklabels (9).

Expected outcome

a boring image :)

Operating system

OS/X

Matplotlib Version

.5.0b1

Matplotlib Backend

MacOSX

Python version

3.9.6

Jupyter version

N/A

Other libraries

No response

Installation

pip

Conda channel

No response

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions