diff --git a/lib/matplotlib/tests/test_tightlayout.py b/lib/matplotlib/tests/test_tightlayout.py index 03ac60a3e0d0..bc535ed88e1f 100644 --- a/lib/matplotlib/tests/test_tightlayout.py +++ b/lib/matplotlib/tests/test_tightlayout.py @@ -264,3 +264,12 @@ def _subplots(): child.set_visible(False) plt.tight_layout() + + +def test_empty_layout(): + """Tests that tight layout doesn't cause an error when there are + no axes. + """ + + fig = plt.gcf() + fig.tight_layout() diff --git a/lib/matplotlib/tight_layout.py b/lib/matplotlib/tight_layout.py index 9ca396dc0e3e..dd363442d9a2 100644 --- a/lib/matplotlib/tight_layout.py +++ b/lib/matplotlib/tight_layout.py @@ -319,6 +319,9 @@ def get_tight_layout_figure(fig, axes_list, subplotspec_list, renderer, subplots.append(ax) + if (len(nrows_list) == 0) or (len(ncols_list) == 0): + return {} + max_nrows = max(nrows_list) max_ncols = max(ncols_list)