-
-
Notifications
You must be signed in to change notification settings - Fork 7.9k
[Bug]: compressed layout setting can be forgotten on second save #24954
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
Yeah we do some dancing around when we save with bbox inches - so this seems to get caught in that. I tried to track it down, but the figure-saving stack is full of context managers, and I can't see where the layout manager gets reset. Hopefully someone more cognizant of that part of the codebase can explain. |
Thanks for looking @jklymak 🙂 |
I think it is set (temporarily) here; matplotlib/lib/matplotlib/backend_bases.py Lines 2356 to 2357 in 018c5ef
|
It is, but I don't understand what |
It calls |
I am way out of my depth here but import matplotlib.pyplot as plt
plt.rcParams['figure.constrained_layout.use'] = True
fig = plt.figure(layout="compressed")
print(fig.get_layout_engine()._compress)
fig.set_layout_engine(fig.get_layout_engine())
print(fig.get_layout_engine()._compress)
fig.savefig('foo.png', bbox_inches='tight')
print(fig.get_layout_engine()._compress)
Without the |
I think the problem is the call to matplotlib/lib/matplotlib/backend_bases.py Lines 2349 to 2350 in 018c5ef
which explicity calls matplotlib/lib/matplotlib/_tight_bbox.py Line 21 in a3011df
which will use the default constrained layout engine if the matplotlib/lib/matplotlib/figure.py Lines 2599 to 2610 in a3011df
|
Bug summary
I'm not sure whether this is really a bug or I'm just using an inconsistent combination of options. Under some specific circumstances (see below) compressed layout is not applied the second time a figure is saved.
Code for reproduction
Actual outcome
test1.png

test2.png

Expected outcome
Both images should look like the first.
Additional information
If I do not set the
rcParams
, all is well. If I do not setbbox_inches="tight"
in my calls tosavefig
, the images are identical (but I have too much white space top and bottom). Maybe there is a better option thanbbox_inches="tight"
when using compressed layout?For context, my real example is a script that makes several figures. For most of them I want constrained layout, so I set that once in the
rcParams
for convenience. Only one figure needs "compressed", and I am saving twice because I want both a png and a pdf. Fixed it in my current example by just reverting thercParams
setting for the one figure.Operating system
RHEL7
Matplotlib Version
3.6.2 and main
Matplotlib Backend
QtAgg
Python version
3.9 and 3.11
Jupyter version
No response
Installation
conda
The text was updated successfully, but these errors were encountered: