From 8522e9ed4fccdfa741296d75c9303b6d3593b512 Mon Sep 17 00:00:00 2001 From: Joshua Barrass Date: Sun, 16 Oct 2022 15:04:00 +0100 Subject: [PATCH 1/5] Do not pass width/height ratios to nested layouts --- lib/matplotlib/figure.py | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/lib/matplotlib/figure.py b/lib/matplotlib/figure.py index 1636e201019b..e7042de249eb 100644 --- a/lib/matplotlib/figure.py +++ b/lib/matplotlib/figure.py @@ -1838,13 +1838,17 @@ def subplot_mosaic(self, mosaic, *, sharex=False, sharey=False, Defines the relative widths of the columns. Each column gets a relative width of ``width_ratios[i] / sum(width_ratios)``. If not given, all columns will have the same width. Equivalent - to ``gridspec_kw={'width_ratios': [...]}``. + to ``gridspec_kw={'width_ratios': [...]}``. This argument applies + only to the outer layout and will not be passed to the inner + layout. height_ratios : array-like of length *nrows*, optional Defines the relative heights of the rows. Each row gets a relative height of ``height_ratios[i] / sum(height_ratios)``. If not given, all rows will have the same height. Equivalent - to ``gridspec_kw={'height_ratios': [...]}``. + to ``gridspec_kw={'height_ratios': [...]}``. This argument + applies only to the outer layout and will not be passed to the + inner layout. subplot_kw : dict, optional Dictionary with keywords passed to the `.Figure.add_subplot` call @@ -1852,7 +1856,9 @@ def subplot_mosaic(self, mosaic, *, sharex=False, sharey=False, gridspec_kw : dict, optional Dictionary with keywords passed to the `.GridSpec` constructor used - to create the grid the subplots are placed on. + to create the grid the subplots are placed on. The 'width_ratios' + and 'height_ratios' keys, if present, will be removed from this + dictionary before it is passed to any inner layouts. empty_sentinel : object, optional Entry in the layout to mean "leave this space empty". Defaults @@ -1999,6 +2005,13 @@ def _do_layout(gs, mosaic, unique_ids, nested): for (j, k), nested_mosaic in nested.items(): this_level[(j, k)] = (None, nested_mosaic, 'nested') + # must remove these two options from the kwargs passed to the + # nested items, otherwise an error occurs if *any* of the inner + # layouts are not compatible with these ratios + nested_gs_kw = dict([(k, v) for k, v in gridspec_kw.items() + if k != "width_ratios" + and k != "height_ratios"]) + # now go through the things in this level and add them # in order left-to-right top-to-bottom for key in sorted(this_level): @@ -2022,7 +2035,7 @@ def _do_layout(gs, mosaic, unique_ids, nested): # recursively add the nested mosaic rows, cols = nested_mosaic.shape nested_output = _do_layout( - gs[j, k].subgridspec(rows, cols, **gridspec_kw), + gs[j, k].subgridspec(rows, cols, **nested_gs_kw), nested_mosaic, *_identify_keys_and_nested(nested_mosaic) ) From 666ab555b670e4ef01fd0ef24f6fbd4b9f87a770 Mon Sep 17 00:00:00 2001 From: Joshua Barrass Date: Sun, 16 Oct 2022 15:28:26 +0100 Subject: [PATCH 2/5] Add test case for width_ratios --- lib/matplotlib/tests/test_figure.py | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/lib/matplotlib/tests/test_figure.py b/lib/matplotlib/tests/test_figure.py index 48b4a880e089..576aad1fb6b6 100644 --- a/lib/matplotlib/tests/test_figure.py +++ b/lib/matplotlib/tests/test_figure.py @@ -922,6 +922,16 @@ def test_nested_tuple(self, fig_test, fig_ref): fig_ref.subplot_mosaic([["F"], [x]]) fig_test.subplot_mosaic([["F"], [xt]]) + def test_nested_width_ratios(self): + x = [["A", [["B"], + ["C"]]]] + width_ratios = [2, 1] + + fig, axd = plt.subplot_mosaic(x, width_ratios=width_ratios) + + assert axd["A"].get_gridspec().get_width_ratios() == width_ratios + assert axd["B"].get_gridspec().get_width_ratios() != width_ratios + @check_figures_equal(extensions=["png"]) @pytest.mark.parametrize( "x, empty_sentinel", From 57a0a01ef976b14aebb69bd89f4b770d55c05ba7 Mon Sep 17 00:00:00 2001 From: Joshua Barrass Date: Sun, 16 Oct 2022 15:30:21 +0100 Subject: [PATCH 3/5] Add test case for height_ratios --- lib/matplotlib/tests/test_figure.py | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/lib/matplotlib/tests/test_figure.py b/lib/matplotlib/tests/test_figure.py index 576aad1fb6b6..d15aaa37ae6f 100644 --- a/lib/matplotlib/tests/test_figure.py +++ b/lib/matplotlib/tests/test_figure.py @@ -932,6 +932,16 @@ def test_nested_width_ratios(self): assert axd["A"].get_gridspec().get_width_ratios() == width_ratios assert axd["B"].get_gridspec().get_width_ratios() != width_ratios + def test_nested_height_ratios(self): + x = [["A", [["B"], + ["C"]]], ["D", "D"]] + height_ratios = [1, 2] + + fig, axd = plt.subplot_mosaic(x, height_ratios=height_ratios) + + assert axd["D"].get_gridspec().get_height_ratios() == height_ratios + assert axd["B"].get_gridspec().get_height_ratios() != height_ratios + @check_figures_equal(extensions=["png"]) @pytest.mark.parametrize( "x, empty_sentinel", From e23ebe348e5a27e997daf1d80b3510acf3fe494b Mon Sep 17 00:00:00 2001 From: Joshua Barrass Date: Sun, 16 Oct 2022 15:44:39 +0100 Subject: [PATCH 4/5] Add behaviour change file --- doc/api/next_api_changes/behavior/24188-JB.rst | 9 +++++++++ 1 file changed, 9 insertions(+) create mode 100644 doc/api/next_api_changes/behavior/24188-JB.rst diff --git a/doc/api/next_api_changes/behavior/24188-JB.rst b/doc/api/next_api_changes/behavior/24188-JB.rst new file mode 100644 index 000000000000..982cdccefc47 --- /dev/null +++ b/doc/api/next_api_changes/behavior/24188-JB.rst @@ -0,0 +1,9 @@ +`fig.subplot_mosaic` no longer passes the `width_ratios` or `height_ratios` args to the nested gridspecs. +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +Previously, the entirety of the `gridspec_kw` dictionary was passed to +the nested gridspecs. This caused an error if the inner layout was +incompatible with one of these ratios. A new dictionary is now made, +`nested_gs_kw`, where the `"width_ratios"` and `"height_ratios"` keys +have been removed, which is passed to any nested gridspecs. This +prevents such errors. From 56b629dd83495cf0d4b67cd5287b919b073d37e0 Mon Sep 17 00:00:00 2001 From: Joshua Barrass Date: Sun, 16 Oct 2022 16:11:44 +0100 Subject: [PATCH 5/5] Double backticks in rst --- doc/api/next_api_changes/behavior/24188-JB.rst | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/doc/api/next_api_changes/behavior/24188-JB.rst b/doc/api/next_api_changes/behavior/24188-JB.rst index 982cdccefc47..c9013392053f 100644 --- a/doc/api/next_api_changes/behavior/24188-JB.rst +++ b/doc/api/next_api_changes/behavior/24188-JB.rst @@ -1,9 +1,9 @@ -`fig.subplot_mosaic` no longer passes the `width_ratios` or `height_ratios` args to the nested gridspecs. +``fig.subplot_mosaic`` no longer passes the ``width_ratios`` or ``height_ratios`` args to the nested gridspecs. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -Previously, the entirety of the `gridspec_kw` dictionary was passed to -the nested gridspecs. This caused an error if the inner layout was +Previously, the entirety of the ``gridspec_kw`` dictionary was passed +to the nested gridspecs. This caused an error if the inner layout was incompatible with one of these ratios. A new dictionary is now made, -`nested_gs_kw`, where the `"width_ratios"` and `"height_ratios"` keys -have been removed, which is passed to any nested gridspecs. This +``nested_gs_kw``, where the ``"width_ratios"`` and ``"height_ratios"`` +keys have been removed, which is passed to any nested gridspecs. This prevents such errors.