Skip to content
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

[Bug]: Setting bbox_inches to a Bbox in fig.savefig resizes colorbar #22625

Closed
pittwolfe opened this issue Mar 8, 2022 · 4 comments · Fixed by #25499
Closed

[Bug]: Setting bbox_inches to a Bbox in fig.savefig resizes colorbar #22625

pittwolfe opened this issue Mar 8, 2022 · 4 comments · Fixed by #25499

Comments

@pittwolfe
Copy link

pittwolfe commented Mar 8, 2022

Bug summary

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

import numpy as np
import matplotlib as mpl
import matplotlib.pyplot as plt

x = 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

bbox_inches

Expected outcome

nobbox_inches

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

Python version

3.9.10

Jupyter version

Jupyter lab 3.2.9

Installation

conda

@jklymak
Copy link
Member

jklymak commented 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
Copy link
Author

Not sure. This is the first time I've tried to set bbox_inches to anything other than "tight".

@oscargus oscargus added this to the v3.7.2 milestone Mar 20, 2023
@irina-nesterova
Copy link

It helps whet the figure is cut on the left and at the top, but doesn'i if it cut the xlabel :(

@rcomer
Copy link
Member

rcomer commented 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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants