-
-
Notifications
You must be signed in to change notification settings - Fork 7.9k
old colorbar removed but still occupies space #15986
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
Pleas try with constrianed_layout. |
Regardless of using constrained_layout or not, I think it's still reasonable to expect a removed colourbar to not affect subsequent colourbar additions. |
Adding a colorbar steals space from the parent. That space remains stolen if you delete the colorbar. If you re add a colorbar the space is stolen a second time. I suppose someone could go through the bother of tracking us a colorbar has already been removed and just put the new colorbar where the old one was. But the use case for needing to do this is not particularly compelling. |
I do not think so. The default layout is static, which is just not suited for the requested behavior. Axes span a certian space of the figure. To prevent overlap, a colorbar steals some space. But that information is lost immediately. The Axes just has a new size. No way of knowing if it was resized by a colorbar or if somebody wanted larger margins. Thus, we cannot undo sizing on colorbar removal or not resize the Axes on a second colorbar addition. Any attempt to do so (e.g. tracking resizing due to colorbars or checking if there is enough space to add colorbar without Axes resizing) would create more trouble than it solves. The requested behavior can only be fulfilled by a real layout manager such as constrained_layout. |
Actually #12443 already (indirectly) fixes this: colorbar.remove already tries to restore the parent's axes ( matplotlib/lib/matplotlib/colorbar.py Lines 1310 to 1313 in b91b001
.ax , not .axes . (Indeed, replacing contour plots in the OP's example by imshows doesn't repro the issue, because AxesImage has a .axes attribute.) So https://github.com/matplotlib/matplotlib/pull/12443/files#diff-2e46ac77d349b367229eda1b93d92fc3R941 is the specific fix for the issue.
|
Bug report
Bug summary
When there are multiple subplots, the colorbar takes space even after successfully removed. New colorbar will have to be positioned to the left of old ones.
It can be seen in the following example that the contour plot is being shrinked in width because space is being occupied by colorbar.
Code for reproduction
Actual outcome
This is the result I get if the last 4 lines are NOT commented; i.e. colorbar
cb
is removed and a new, identical one is added.If I do this process (remove -> readd) again, things get even worse,

Expected outcome

This is the result I get if the last 4 lines ARE commented; i.e., only 1 colorbar is ever created
Matplotlib version
The text was updated successfully, but these errors were encountered: