Setting bbox_inches in fig.savefig to a specified Bbox rather than "tight" resizes the colorbar relative to when bbox_inches is not set—the resulting colorbar is a lot larger than it should be.
Code for reproduction
importnumpyasnpimportmatplotlibasmplimportmatplotlib.pyplotaspltx=np.linspace(0, 2*np.pi, 100)
y=np.linspace(0, 2*np.pi, 100)
X, Y=np.meshgrid(x,y)
fig, ax=plt.subplots(figsize=(4,4))
pc=ax.pcolormesh(x, y, np.sin(X)*np.sin(Y))
fig.colorbar(pc, ax=ax, aspect=40)
# Uncomment the first fig.savefig to get the correct output on the second call# fig.savefig('nobbox_inches.png')fig.savefig('bbox_inches.png', bbox_inches=mpl.transforms.Bbox([[0, 0], [4, 4]]))
Actual outcome
Expected outcome
Additional information
Calling fig.savefig without bbox_inches set first and then calling it again with bbox_inches set produces expected outcome.
Calling plt.show() prior to savefig also works.
Operating system
MacOS 12.2.1 (Monterey)
Matplotlib Version
3.5.1
Matplotlib Backend
inline (for interactive), png for plot, also a problem with pdf
@irina-nesterova if you are still having problems with this in Matplotlib v3.7.2, please open a new issue with some code that demonstrates the problem.
Activity
jklymak commented on Mar 8, 2022
Do you know if this ever worked? We changed some colorbar things recently, but not sure what would have caused this to happen. Thanks
pittwolfe commented on Mar 8, 2022
Not sure. This is the first time I've tried to set bbox_inches to anything other than "tight".
irina-nesterova commented on Sep 1, 2023
It helps whet the figure is cut on the left and at the top, but doesn'i if it cut the xlabel :(
rcomer commented on Sep 1, 2023
@irina-nesterova if you are still having problems with this in Matplotlib v3.7.2, please open a new issue with some code that demonstrates the problem.