Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Repeated calls to tight_layout needed with aspect='equal' #18313

Copy link
Copy link
Closed
@JoElfner

Description

@JoElfner
Issue body actions

Bug report

Producing 4 subplots with a rectangular figure (height > width) and setting the subplots to ax.set_aspect('equal') causes fig.tight_layout(pad=0., h_pad=0., w_pad=0.) to introduce a huge vertical gap between the subplots on the first call.
Each subsequent call to tight_layout reduces the gap slowly, until the set arguments pad and h_pad are satisfied.

Code for reproduction

import matplotlib.pyplot as plt
import numpy as np

randarr = np.random.rand(200, 4)
txtbox = 'this is some\ntext with multiple\nlines and absolutely\nno meaning'

fig, ((a1, a2), (a3, a4)) = plt.subplots(2, 2, figsize=(16/2.54, 25/2.54))
a1.scatter(randarr[:, 0] * 3, randarr[:, 1] * 3, label='a\nb')
a2.scatter(randarr[:, 1] * 2, randarr[:, 2] * 2, label='b\nc')
a3.scatter(randarr[:, 2] * 2, randarr[:, 3] * 2, label='c\nd')
a4.scatter(randarr[:, 3] / 3, randarr[:, 0] / 3, label='d\ne')

_ = [_ax.set_aspect('equal') for _ax in (a1, a2, a3, a4)]
fig.tight_layout(pad=0., h_pad=1., w_pad=1.)

To reduce the vertical gap until padding is satisfied:

for _ in range(10):
    fig.tight_layout(pad=0., h_pad=1., w_pad=1.)    

And it seems that for plots with a high complexity, calls to fig.canvas.draw are required between the calls to tight_layout. But I cannot reproduce this reliably:

for _ in range(10):
    fig.tight_layout(pad=0., h_pad=1., w_pad=1.)    
    fig.canvas.draw()

Actual outcome
The outcome of the code without repeated calls to tight_layout:
scttr_false_output

Expected outcome

The in my opinion correct output can be produced with:

import matplotlib.pyplot as plt
import numpy as np

randarr = np.random.rand(200, 4)
txtbox = 'this is some\ntext with multiple\nlines and absolutely\nno meaning'

fig, ((a1, a2), (a3, a4)) = plt.subplots(2, 2, figsize=(16/2.54, 25/2.54))
a1.scatter(randarr[:, 0] * 3, randarr[:, 1] * 3, label='a\nb')
a2.scatter(randarr[:, 1] * 2, randarr[:, 2] * 2, label='b\nc')
a3.scatter(randarr[:, 2] * 2, randarr[:, 3] * 2, label='c\nd')
a4.scatter(randarr[:, 3] / 3, randarr[:, 0] / 3, label='d\ne')

_ = [_ax.set_aspect('equal') for _ax in (a1, a2, a3, a4)]
for _ in range(10):
    fig.tight_layout(pad=0., h_pad=1., w_pad=1.)

Expected output:
scttr_correct_output

Matplotlib version

  • Operating system: Windows 10 64bit
  • Matplotlib version: 3.3.1
  • Matplotlib backend: Qt5Agg
  • Python version: 3.7.7
  • Other libraries: Numpy v 1.19.1

All packages are installed from the default conda channel.

Metadata

Metadata

Assignees

No one assigned

    Labels

    status: inactiveMarked by the “Stale” Github ActionMarked by the “Stale” Github Actiontopic: geometry managerLayoutEngine, Constrained layout, Tight layoutLayoutEngine, Constrained layout, Tight layout

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      Morty Proxy This is a proxified and sanitized view of the page, visit original site.