Movatterモバイル変換


[0]ホーム

URL:


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

Sign up
Appearance settings

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

Closed
Labels
status: inactiveMarked by the “Stale” Github Actiontopic: geometry managerLayoutEngine, Constrained layout, Tight layout
@JoElfner

Description

@JoElfner

Bug report

Producing 4 subplots with a rectangular figure (height > width) and setting the subplots toax.set_aspect('equal') causesfig.tight_layout(pad=0., h_pad=0., w_pad=0.) to introduce ahuge vertical gap between the subplots on the first call.
Each subsequent call totight_layout reduces the gap slowly, until the set argumentspad andh_pad are satisfied.

Code for reproduction

import matplotlib.pyplot as pltimport numpy as nprandarr = 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 tofig.canvas.draw are required between the calls totight_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 pltimport numpy as nprandarr = 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 Actiontopic: geometry managerLayoutEngine, Constrained layout, Tight layout

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions


      [8]ページ先頭

      ©2009-2025 Movatter.jp