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