Skip to content

[Bug]: Error using width_ratios with nested mosaic in subplot_mosaic() #24099

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

Closed
bneary3 opened this issue Oct 5, 2022 · 2 comments · Fixed by #24189
Closed

[Bug]: Error using width_ratios with nested mosaic in subplot_mosaic() #24099

bneary3 opened this issue Oct 5, 2022 · 2 comments · Fixed by #24189
Labels
Good first issue Open a pull request against these issues if there are no active ones! status: confirmed bug
Milestone

Comments

@bneary3
Copy link

bneary3 commented Oct 5, 2022

Bug summary

plt.subplot_mosaic() fails with a confusing error message when passing width_ratios (or height_ratios) with a nested list mosaic, unless all outer and inner mosaics have the same number of columns (or rows).

Code for reproduction

import matplotlib.pyplot as plt

mosaic = [["A", [["B"],
                 ["C"]]]]

fig, axd = plt.subplot_mosaic(mosaic, width_ratios=[2, 1])

Actual outcome

Traceback (most recent call last):
  File "C:\Users\bneary3\test_layouts.py", line 6, in <module>
    fig, axd = plt.subplot_mosaic(mosaic, width_ratios=[2, 1])
  File "C:\Users\bneary3\Anaconda3\envs\mpl36\lib\site-packages\matplotlib\pyplot.py", line 1533, in subplot_mosaic
    ax_dict = fig.subplot_mosaic(
  File "C:\Users\bneary3\Anaconda3\envs\mpl36\lib\site-packages\matplotlib\figure.py", line 2042, in subplot_mosaic
    ret = _do_layout(gs, mosaic, *_identify_keys_and_nested(mosaic))
  File "C:\Users\bneary3\Anaconda3\envs\mpl36\lib\site-packages\matplotlib\figure.py", line 2023, in _do_layout
    gs[j, k].subgridspec(rows, cols, **gridspec_kw),
  File "C:\Users\bneary3\Anaconda3\envs\mpl36\lib\site-packages\matplotlib\gridspec.py", line 749, in subgridspec
    return GridSpecFromSubplotSpec(nrows, ncols, self, **kwargs)
  File "C:\Users\bneary3\Anaconda3\envs\mpl36\lib\site-packages\matplotlib\gridspec.py", line 516, in __init__
    super().__init__(nrows, ncols,
  File "C:\Users\bneary3\Anaconda3\envs\mpl36\lib\site-packages\matplotlib\gridspec.py", line 56, in __init__
    self.set_width_ratios(width_ratios)
  File "C:\Users\bneary3\Anaconda3\envs\mpl36\lib\site-packages\matplotlib\gridspec.py", line 111, in set_width_ratios
    raise ValueError('Expected the given number of width ratios to '
ValueError: Expected the given number of width ratios to match the number of columns of the grid

Expected outcome

image

Additional information

From what I can tell, this happens because the layout is built recursively, passing the same gridspec_kw to subgridspec() at each level of nesting. I realize that the use of width_ratios / height_ratios / gridspec_kw with nested list mosaics could get complicated, but it would be nice to be able to at least specify them for the outer list, or in some other way implement this feature for nested list layouts. If not, maybe include a helpful error message that explains this limitation so the user doesn't think they have specified the wrong number of ratios.

Operating system

Windows 10

Matplotlib Version

3.6.0

Matplotlib Backend

QtAgg

Python version

3.10

Jupyter version

No response

Installation

conda

@timhoffm
Copy link
Member

timhoffm commented Oct 6, 2022

I advocate that passing these kwargs to nested definitions is simply a bug. We should only apply them to the outermost grid. - Typically it does not make sense to use the same parameters for inner definitions even if they are compatible.

Defining parameters for inner layouts might be added later by supporting nested datastructures for gridspec_kw. width_ratios / height_ratios are only a convenience layer and don't need to handle complex cases such as passing parameters to inner layouts.

@timhoffm timhoffm added the Good first issue Open a pull request against these issues if there are no active ones! label Oct 16, 2022
@timhoffm
Copy link
Member

Marking as good first issue.

Things to do:

  • do not pass width_ratios, height_ratios to nested layouts
  • document this in the parameter descriptions
  • add a test: subplot_mosaic([['A', [['B', 'C']]]], width_ratios=[2, 1]) should be good. You can check that the width ratios of the outer layout is [2, 1] but of the inner is not.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Good first issue Open a pull request against these issues if there are no active ones! status: confirmed bug
Projects
None yet
3 participants