8000 Fix tight_layout to handle empty figures · matplotlib/matplotlib@c34a74d · GitHub
[go: up one dir, main page]

Skip to content

Commit c34a74d

Browse files
committed
Fix tight_layout to handle empty figures
1 parent 8e517d8 commit c34a74d

File tree

3 files changed

+13
-0
lines changed

3 files changed

+13
-0
lines changed

lib/matplotlib/figure.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1973,6 +1973,8 @@ def tight_layout(self, renderer=None, pad=1.08, h_pad=None,
19731973
renderer,
19741974
pad=pad, h_pad=h_pad, w_pad=w_pad,
19751975
rect=rect)
1976+
if kwargs is None:
1977+
return
19761978

19771979
self.subplots_adjust(**kwargs)
19781980

lib/matplotlib/tests/test_tightlayout.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,3 +264,11 @@ def _subplots():
264264
child.set_visible(False)
265265

266266
plt.tight_layout()
267+
268+
def test_empty_layout():
269+
"""Tests that tight layout doesn't cause an error when there are
270+
no axes.
271+
"""
272+
273+
fig = plt.gcf()
274+
fig.tight_layout()

lib/matplotlib/tight_layout.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -319,6 +319,9 @@ def get_tight_layout_figure(fig, axes_list, subplotspec_list, renderer,
319319

320320
subplots.append(ax)
321321

322+
if (len(nrows_list) == 0) or (len(ncols_list) == 0):
323+
return
324+
322325
max_nrows = max(nrows_list)
323326
max_ncols = max(ncols_list)
324327

0 commit comments

Comments
 (0)
0