Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[Bug]: compressed layout setting can be forgotten on second save #24954

Closed
@rcomer

Description

@rcomer

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

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

arr = np.arange(100).reshape((10, 10))

matplotlib.rcParams['figure.constrained_layout.use'] = True

fig, ax_dict = plt.subplot_mosaic('AB;AC', figsize=(6, 9), width_ratios=[3, 2],
                                  layout='compressed')

for key in ["B", "C"]:
    ax_dict[key].imshow(arr)
    
fig.savefig("test1.png", bbox_inches="tight")
fig.savefig("test2.png", bbox_inches="tight")

Actual outcome

test1.png
test1

test2.png
test2

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 set bbox_inches="tight" in my calls to savefig, the images are identical (but I have too much white space top and bottom). Maybe there is a better option than bbox_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 the rcParams 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

Metadata

Metadata

Assignees

No one assigned

    Labels

    topic: geometry managerLayoutEngine, Constrained layout, Tight layout

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions