Skip to content

[Bug]: constrained_layout merging similar subgrids #22143

Closed
@malfatti

Description

@malfatti

Bug summary

When plotting a figure with constrained_layout and subgrids, rows with the same arguments are being plot on top of each other.

Code for reproduction

import matplotlib.pyplot as plt

Fig = plt.figure(constrained_layout=True)

GS = Fig.add_gridspec(3,1)
GSRows =  [
    GS[0].subgridspec(1,5, width_ratios=(0.2, 1, 1, 1, 0.2)),
    GS[1].subgridspec(1,5, width_ratios=(0.2, 1, 0.2, 1, 1)),
    GS[2].subgridspec(1,5, width_ratios=(0.2, 1, 1, 1, 0.2)),
    # GS[2].subgridspec(1,5, width_ratios=(0.199999, 1, 1, 1, 0.2)),
]

Axes = [
    Fig.add_subplot(GSRows[0][1]),
    Fig.add_subplot(GSRows[0][2]),
    Fig.add_subplot(GSRows[0][3]),

    Fig.add_subplot(GSRows[1][1]),
    Fig.add_subplot(GSRows[1][3]),
    Fig.add_subplot(GSRows[1][4]),

    Fig.add_subplot(GSRows[2][1]),
    Fig.add_subplot(GSRows[2][2]),
    Fig.add_subplot(GSRows[2][3]),
]

Fig.savefig('ActualOutcome.jpg')
# Fig.savefig('ExpectedOutcome.jpg')

plt.show()

Actual outcome

ActualOutcome

Expected outcome

(Changed the third row first column width ratio from 0.2 to 0.199999, see commented line)
ExpectedOutcome

Additional information

In the example, the figure has 3 rows and 5 columns with different width ratios. Rows 1 and 3 have the same width ratios, while row 2 has a different width ratio. Note that rows 1 and 3 are merged (row 3 is drawn on top of row 1; Actual outcome image). Note also that if the subgrids are all different (see commented lines in code example changing the width ratio of row 3 from 0.2 to 0.199999) all three rows are drawn as expected.

This was not the case at least until 3.4.3, where even if the rows are equal, they are drawn at their expected places.

Operating system

Gentoo

Matplotlib Version

3.5.1

Matplotlib Backend

TkAgg

Python version

Python 3.9.9

Jupyter version

6.4.6

Installation

Linux package manager

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