Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bug]: Mosaic Scaling #23290

Open
lswenson opened this issue Jun 16, 2022 · 7 comments
Open

[Bug]: Mosaic Scaling #23290

lswenson opened this issue Jun 16, 2022 · 7 comments

Comments

@lswenson
Copy link

@lswenson lswenson commented Jun 16, 2022

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 xarray as xr
import numpy as np
import matplotlib.pyplot as plt
import matplotlib as mpl
import cartopy.crs as ccrs
import cartopy.feature as cfeature

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

@lswenson lswenson changed the title [Bug]: [Bug]: Mosaic Scaling Jun 16, 2022
@tacaswell
Copy link
Member

@tacaswell tacaswell commented Jun 16, 2022

attn @jklymak

@jklymak
Copy link
Member

@jklymak jklymak commented Jun 17, 2022

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]]

@jklymak
Copy link
Member

@jklymak jklymak commented Jun 17, 2022

... 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.

@jklymak
Copy link
Member

@jklymak jklymak commented Jun 18, 2022

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

@timhoffm
Copy link
Member

@timhoffm timhoffm commented Jun 18, 2022

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

@jklymak
Copy link
Member

@jklymak jklymak commented Jun 18, 2022

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.

@timhoffm
Copy link
Member

@timhoffm timhoffm commented Jun 18, 2022

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants