Skip to content

Commit 3a5c6dc

Browse files
joshbarrassjklymaktimhoffm
authored
Do not pass gridspec_kw to inner layouts in subplot_mosaic (#24189)
* Do not pass width/height ratios to nested layouts Co-authored-by: Jody Klymak <jklymak@gmail.com> Co-authored-by: Tim Hoffmann <2836374+timhoffm@users.noreply.github.com>
1 parent 7b08bee commit 3a5c6dc

File tree

3 files changed

+39
-4
lines changed

3 files changed

+39
-4
lines changed
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
``fig.subplot_mosaic`` no longer passes the ``gridspec_kw`` args to nested gridspecs.
2+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
3+
4+
For nested `.Figure.subplot_mosaic` layouts, it is almost always
5+
inappropriate for *gridspec_kw* arguments to be passed to lower nest
6+
levels, and these arguments are incompatible with the lower levels in
7+
many cases. This dictionary is no longer passed to the inner
8+
layouts. Users who need to modify *gridspec_kw* at multiple levels
9+
should use `.Figure.subfigures` to get nesting, and construct the
10+
inner layouts with `.Figure.subplots` or `.Figure.subplot_mosaic`.

lib/matplotlib/figure.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1834,21 +1834,26 @@ def subplot_mosaic(self, mosaic, *, sharex=False, sharey=False,
18341834
Defines the relative widths of the columns. Each column gets a
18351835
relative width of ``width_ratios[i] / sum(width_ratios)``.
18361836
If not given, all columns will have the same width. Equivalent
1837-
to ``gridspec_kw={'width_ratios': [...]}``.
1837+
to ``gridspec_kw={'width_ratios': [...]}``. In the case of nested
1838+
layouts, this argument applies only to the outer layout.
18381839
18391840
height_ratios : array-like of length *nrows*, optional
18401841
Defines the relative heights of the rows. Each row gets a
18411842
relative height of ``height_ratios[i] / sum(height_ratios)``.
18421843
If not given, all rows will have the same height. Equivalent
1843-
to ``gridspec_kw={'height_ratios': [...]}``.
1844+
to ``gridspec_kw={'height_ratios': [...]}``. In the case of nested
1845+
layouts, this argument applies only to the outer layout.
18441846
18451847
subplot_kw : dict, optional
18461848
Dictionary with keywords passed to the `.Figure.add_subplot` call
18471849
used to create each subplot.
18481850
18491851
gridspec_kw : dict, optional
18501852
Dictionary with keywords passed to the `.GridSpec` constructor used
1851-
to create the grid the subplots are placed on.
1853+
to create the grid the subplots are placed on. In the case of
1854+
nested layouts, this argument applies only to the outer layout.
1855+
For more complex layouts, users should use `.Figure.subfigures`
1856+
to create the nesting.
18521857
18531858
empty_sentinel : object, optional
18541859
Entry in the layout to mean "leave this space empty". Defaults
@@ -2018,7 +2023,7 @@ def _do_layout(gs, mosaic, unique_ids, nested):
20182023
# recursively add the nested mosaic
20192024
rows, cols = nested_mosaic.shape
20202025
nested_output = _do_layout(
2021-
gs[j, k].subgridspec(rows, cols, **gridspec_kw),
2026+
gs[j, k].subgridspec(rows, cols),
20222027
nested_mosaic,
20232028
*_identify_keys_and_nested(nested_mosaic)
20242029
)

lib/matplotlib/tests/test_figure.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -922,6 +922,26 @@ def test_nested_tuple(self, fig_test, fig_ref):
922922
fig_ref.subplot_mosaic([["F"], [x]])
923923
fig_test.subplot_mosaic([["F"], [xt]])
924924

925+
def test_nested_width_ratios(self):
926+
x = [["A", [["B"],
927+
["C"]]]]
928+
width_ratios = [2, 1]
929+
930+
fig, axd = plt.subplot_mosaic(x, width_ratios=width_ratios)
931+
932+
assert axd["A"].get_gridspec().get_width_ratios() == width_ratios
933+
assert axd["B"].get_gridspec().get_width_ratios() != width_ratios
934+
935+
def test_nested_height_ratios(self):
936+
x = [["A", [["B"],
937+
["C"]]], ["D", "D"]]
938+
height_ratios = [1, 2]
939+
940+
fig, axd = plt.subplot_mosaic(x, height_ratios=height_ratios)
941+
942+
assert axd["D"].get_gridspec().get_height_ratios() == height_ratios
943+
assert axd["B"].get_gridspec().get_height_ratios() != height_ratios
944+
925945
@check_figures_equal(extensions=["png"])
926946
@pytest.mark.parametrize(
927947
"x, empty_sentinel",

0 commit comments

Comments
 (0)