Skip to content

[Bug]: tight_layout (version 3.5+) #22673

Closed
@2sn

Description

@2sn

Bug summary

fig.tight_layout is broken in current matplotlib.

Code for reproduction

%pylab
plot([0,1])
tight_layout()

# happens same way in object mode
from matplotlib import pylab as plt
fig, ax = plt.subplots()
ax.plot([0,1])
fig.tight_layout()

Actual outcome

xxx

below another example from real code that may be instructive

xxx

Expected outcome

figure scaled to image size, background deleted.

Additional information

Happens always.

I do not recall this issue in version before 3.5

I have no clue why it is happening. The flaw seems to lay in mpl getting/setting wrong window size when using tight_layout. Maybe gtk unhappy with X11. When I use mpl.use('Qt5Cairo') then it just makes the window and fonts smaller. Obviously, tight_layout should not change physical window size on screen! Similar for Gtk4Cairo. Gtk4Agg has similar issues to Qt5Agg but does not retain noise in background, just shrinks figure. I could be related to DPI settings. It might get them from a different system call than when opening the window?

The only fix I can find is use other plot package, e.g., plotly.

Operating system

Linux 5.16.14-200.fc35.x86_64 #1 SMP PREEMPT Fri Mar 11 20:31:18 UTC 2022 x86_64 x86_64 x86_64 GNU/Linux

Matplotlib Version

3.5.1

Matplotlib Backend

Qt5Agg

Python version

Python 3.10.2

Jupyter version

N/A

Installation

pip

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions