Repository navigation
Description
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
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

Activity
timhoffm commentedon 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 commentedon Oct 16, 2022
Marking as good first issue.
Things to do:
width_ratios,height_ratiosto nested layoutssubplot_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.