-
-
Notifications
You must be signed in to change notification settings - Fork 7.9k
[Bug]: regression with setting ticklabels for colorbars in matplotlib 3.5.0b1 #20989
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
Comments
A bit of diving and this is because:
So there are two things going on here:
At this point I'm not sure whether the fix is to change 1. or 2. or neither here, perhaps someone else can chime in with that 😄 |
Thank you so much for digging into this so promptly ! I have to say I would be very happy to learn a more idiomatic way to rotate ticklabels on colorbar, but I note that this stack overflow post is the first result when I google this again: so it's very likely been cargo culted into the module where I discovered this, and I wouldn't be surprised if it was also blindly copy-pasted in other codes downstream. |
What you want to achieve is much simpler with 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.tick_params(rotation=45) |
Excellent, thanks @QuLogic ! I posted it as an answer in the SO post linked above, in case you'd like to upvote it https://stackoverflow.com/a/69053022/5489622 |
…tate tick labels in cbar (#24) see matplotlib/matplotlib#20989
I guess I think this is a bug in get_xticklabels. If we expect ticks and labels to always be in sync, the get should also return what we would give the set. I don't understand what the locator and formatter are doing here exactly. Just to be clear in #20054 I tried not to change too much, and don't make any claim to understand all the choices. But presumably the same discrepancy is present with a normal x axis. |
actually it's different. import matplotlib.pyplot as plt
fig, ax = plt.subplots()
ax.set_xticklabels(ax.get_xticklabels())
fig.savefig("/tmp/20989.png") output
note that this behaviour is consistent from matplotlib 3.4.3 to the current latest commit on the main branch (d358cc3), so presumably no one cares. However, this indicates that the problem I reported is indeed exclusive to colorbar axes. |
The reproducer on a normal axes is: fig, ax = plt.subplots()
ax.set_xlim(0, 1)
ax.xaxis.set_major_locator(FixedLocator(np.linspace(0, 1, 51), nbins=10))
ax.set_xticklabels(ax.get_xticklabels())
plt.show() Which gives:
If we think the incantation above is acceptable, then this is a bug in |
... But I think that magical incantation never worked for normal axes because the text is not populated until after a draw. It used to work for colorbars because they manually populated the ticks, but the whole point of #20054 was to make the axes more similar in behaviour. So, 1) axis.py needs a small change to use Overall I don't think this "regression" is going to go away, and given it never worked for normal axises, I'm tempted to accept the breakage here which just happened to work unintentionally for colorbars. |
Also please note that |
Sounds reasonable to me, I wouldn't want this to block the 3.5 release |
Lets close as "won't fix" - this does not work on "normal" axes with Fixed locators and |
Uh oh!
There was an error while loading. Please reload this page.
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:
Admittedly this line seems like it should not do anything and certainly not produce an error.
In practice, my actual application contains the following:
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
Actual outcome
Traceback
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
The text was updated successfully, but these errors were encountered: