Skip to content

[Bug]: Constrained Layout scaling of layouts with submerged spines #23290

Closed

Description

@lswenson

Bug summary

I think "constrained_layout=True" breaks the mosaic scaling. For example subplot D is smaller than subplot E in the "Actual Outcome". When I use plot.tight_layout() and contrained_layout=False the subplots are the same size as in the "Expected Outcome"

Code for reproduction

import matplotlib.pyplot as plt
import matplotlib as mpl

def identify_axes(ax_dict, fontsize=48):
    """
    Helper to identify the Axes in the examples below.

    Draws the label in a large font in the center of the Axes.

    Parameters
    ----------
    ax_dict : dict[str, Axes]
        Mapping between the title / label and the Axes.
    fontsize : int, optional
        How big the label should be.
    """
    kw = dict(ha="center", va="center", fontsize=fontsize, color="darkgrey")
    for k, ax in ax_dict.items():
        ax.text(0.5, 0.5, k, transform=ax.transAxes, **kw)

mosaic = """AAADDD
            AAADDD
            BBBDDD
            BBBEEE
            CCCEEE
            CCCEEE"""
fig = plt.figure(constrained_layout=True)
ax_dict = fig.subplot_mosaic(mosaic)
identify_axes(ax_dict)

Actual outcome

Screen Shot 2022-06-16 at 14 59 13

Expected outcome

Screen Shot 2022-06-16 at 15 02 03

Additional information

No response

Operating system

OS/X

Matplotlib Version

3.5.1

Matplotlib Backend

module://matplotlib_inline.backend_inline

Python version

3.9.12

Jupyter version

3.3.2

Installation

conda

Activity

  1. changed the title [-][Bug]: [/-] [+][Bug]: Mosaic Scaling[/+] on Jun 16, 2022
  2. tacaswell commented on Jun 16, 2022

    @tacaswell
    Member
  3. jklymak commented on Jun 17, 2022

    @jklymak
    Member

    Thanks for the report. The dance that has to happen when axes have staggered limits like this is a bit delicate, and clearly doesn't work in this case. Its probably fixable.

    What does work is to keep the two in a separate subfigures:

    left = [['L1'], ['L2']]
    right = [['R1'], ['R2'], ['R3']]
    mosaic = [[left, right]]
  4. added this to the v3.6.0 milestone on Jun 17, 2022
  5. jklymak commented on Jun 17, 2022

    @jklymak
    Member

    ... there is a workaround, so I'll milestone 3.6, though it could easily go in a 3.6 point release as well. I probably can fix this, but it may take some tinkering.

  6. jklymak commented on Jun 18, 2022

    @jklymak
    Member

    I'm tempted to leave this as wont-fix. In this case, the left hand column has no internal spines in common w/ the righthand column, so it is not particularly useful for them to be in the same layout, and constrained_layout was designed to make lined-up layouts taking into account different decorations on each axes. tight_layout just makes all the spaces around the axes the same, and set by the largest decoration

    I'm not arguing that what the OP was doing was at all wrong, but just one of those cases for which constrained_layout is ill-suited.

    Again, if you use subfigures or nested mosaics you can get what you want, or you can fallback on tight_layout.

    We can keep this open because we should document this case as a time when constrained_layout fails for the constrainedlayout_guide.py

  7. timhoffm commented on Jun 18, 2022

    @timhoffm
    Member

    @jklymak is there a way to detect such cases so that we can issue a waning, possibly with a hint what to do instead.

  8. jklymak commented on Jun 18, 2022

    @jklymak
    Member

    I'm not sure. It's probably possible to post process the positions returned and decide if there are problems. The array of possible problems is pretty undefined, however.

    Just to be clear; constrained layout did what we told it to here - it's just that when you span a subplot across subplotspec slots it gets the margins it subsumes as well. Because there are an odd number is subplots in the left column the right column lines up differently for the two subplots.

    I think the general warning is that spanning subplotspecs works for many cases, but is fragile in general.

  9. timhoffm commented on Jun 18, 2022

    @timhoffm
    Member

    The detection does not have to be exhausting. We don't want false-positive alerts. But it's ok not to wan in every problematic situation. It's already helpful to detect some common specific cases and warn on them.

  10. jklymak commented on Jul 27, 2022

    @jklymak
    Member

    I changed the status here, I suspect this can be fixed, and I'll try to get to it over the next few weeks...

  11. modified the milestones: v3.6.0, v3.7.0 on Aug 24, 2022
  12. changed the title [-][Bug]: Mosaic Scaling[/-] [+][Bug]: Constrained Layout scaling of layouts with submerged spines[/+] on Mar 7, 2023
  13. github-actions commented on May 4, 2026

    @github-actions

    This issue has been marked "inactive" because it has been 365 days since the last comment. If this issue is still present in recent Matplotlib releases, or the feature request is still wanted, please leave a comment and this label will be removed. If there are no updates in another 30 days, this issue will be automatically closed, but you are free to re-open or create a new issue if needed. We value issue reports, and this procedure is meant to help us resurface and prioritize issues that have not been addressed yet, not make them disappear. Thanks for your help!

  14. rcomer commented on May 4, 2026

    @rcomer
    Member

    This would be fixed by #30108, but I'm not clear how.

    Image
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions