When adding multiple colorbars with plt.colorbar to different subplots and then removing them again, the removal affects the width of the other subplot.
It is set correctly, it is "just" that the logic doesn't consider this case.
The code assumes that the mappable provided (the scatter plot) is also the object that got the colorbar attached to it and therefore the part that should be updated when the colorbar is removed. Probably one should use ax instead here. (Althiguh that may cause other issues...)
Thanks @oscargus that does indeed seem to be the problem. I'm skeptical we should resize anything, but if that is the existing behaviour we should be storing the result of ax=ax and operating on that (if it is a single axes).
The problem is that the colorbar axes is created outside of colorbar, so the colorbar method never knows if it stole from space anyone or not. I think its overreach for colorbar to then try to give space back...
This issue has been marked "inactive" because it has been 365 days since the last comment. If this issue is still present in recent Matplotlib releases, or the feature request is still wanted, please leave a comment and this label will be removed. If there are no updates in another 30 days, this issue will be automatically closed, but you are free to re-open or create a new issue if needed. We value issue reports, and this procedure is meant to help us resurface and prioritize issues that have not been addressed yet, not make them disappear. Thanks for your help!
Activity
oscargus commented on Jul 29, 2022
Indeed! If anything, the bottom subplot should expand. Definitely not the top one.
oscargus commented on Jul 29, 2022
The problem seems to be that
matplotlib/lib/matplotlib/colorbar.py
Line 1087 in 1bac3b3
points at the wrong axis, i.e. the top one. Which leads to that the rest of the code is applied to the top rather than the bottom axis.
matplotlib/lib/matplotlib/colorbar.py
Lines 1090 to 1099 in 1bac3b3
So self.mappable.axes is not set correctly(?).
oscargus commented on Jul 29, 2022
It is set correctly, it is "just" that the logic doesn't consider this case.
The code assumes that the mappable provided (the scatter plot) is also the object that got the colorbar attached to it and therefore the part that should be updated when the colorbar is removed. Probably one should use ax instead here. (Althiguh that may cause other issues...)
jklymak commented on Jul 29, 2022
Thanks @oscargus that does indeed seem to be the problem. I'm skeptical we should resize anything, but if that is the existing behaviour we should be storing the result of ax=ax and operating on that (if it is a single axes).
The problem is that the colorbar axes is created outside of colorbar, so the colorbar method never knows if it stole from space anyone or not. I think its overreach for colorbar to then try to give space back...
jklymak commented on Dec 11, 2022
Just to clarify here:
Does what I think the OP wants.
If they really don't want to use constrained layout, they can also do various other hacks (inset_axes is the best way), or make a mock scalarmappable
github-actions commented on Feb 2, 2026
This issue has been marked "inactive" because it has been 365 days since the last comment. If this issue is still present in recent Matplotlib releases, or the feature request is still wanted, please leave a comment and this label will be removed. If there are no updates in another 30 days, this issue will be automatically closed, but you are free to re-open or create a new issue if needed. We value issue reports, and this procedure is meant to help us resurface and prioritize issues that have not been addressed yet, not make them disappear. Thanks for your help!
timhoffm commented on Feb 2, 2026
Duplicate #22257