Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit a60c37c

Browse files
authored
Merge pull request #11631 from jklymak/doc-better-tl-error
DOC: better tight_layout error handling
2 parents 66d2282 + 3031da0 commit a60c37c

File tree

2 files changed

+21
-2
lines changed

2 files changed

+21
-2
lines changed

lib/matplotlib/tests/test_tightlayout.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -305,3 +305,14 @@ def test_big_decorators_vertical():
305305
with warnings.catch_warnings(record=True) as w:
306306
fig.tight_layout()
307307
assert len(w) == 1
308+
309+
310+
def test_badsubplotgrid():
311+
# test that we get warning for mismatched subplot grids rather
312+
# than an error
313+
ax1 = plt.subplot2grid((4, 5), (0, 0))
314+
# this is the bad entry:
315+
ax5 = plt.subplot2grid((5, 5), (0, 3), colspan=3, rowspan=5)
316+
with warnings.catch_warnings(record=True) as w:
317+
plt.tight_layout()
318+
assert len(w) == 1

lib/matplotlib/tight_layout.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -326,8 +326,16 @@ def get_tight_layout_figure(fig, axes_list, subplotspec_list, renderer,
326326
rows, cols, num1, num2 = subplotspec.get_geometry()
327327
div_row, mod_row = divmod(max_nrows, rows)
328328
div_col, mod_col = divmod(max_ncols, cols)
329-
if (mod_row != 0) or (mod_col != 0):
330-
raise RuntimeError("")
329+
if mod_row != 0:
330+
warnings.warn('tight_layout not applied: '
331+
'number of rows in subplot specifications must'
332+
'be multiples of one another.')
333+
return {}
334+
if mod_col != 0:
335+
warnings.warn('tight_layout not applied: '
336+
'number of columns in subplot specifications must'
337+
'be multiples of one another.')
338+
return {}
331339

332340
rowNum1, colNum1 = divmod(num1, cols)
333341
if num2 is None:

0 commit comments

Comments
 (0)