diff --git a/lib/matplotlib/tests/test_tightlayout.py b/lib/matplotlib/tests/test_tightlayout.py index f7e571958610..0516971d2d9c 100644 --- a/lib/matplotlib/tests/test_tightlayout.py +++ b/lib/matplotlib/tests/test_tightlayout.py @@ -272,3 +272,41 @@ def test_empty_layout(): fig = plt.gcf() fig.tight_layout() + + +def test_verybig_decorators_horizontal(): + "Test that warning emitted when xlabel too big" + fig, ax = plt.subplots(figsize=(3, 2)) + ax.set_xlabel('a' * 100) + with warnings.catch_warnings(record=True) as w: + fig.tight_layout() + assert len(w) == 1 + + +def test_verybig_decorators_vertical(): + "Test that warning emitted when xlabel too big" + fig, ax = plt.subplots(figsize=(3, 2)) + ax.set_ylabel('a' * 100) + with warnings.catch_warnings(record=True) as w: + fig.tight_layout() + assert len(w) == 1 + + +def test_big_decorators_horizontal(): + "Test that warning emitted when xlabel too big" + fig, axs = plt.subplots(1, 2, figsize=(3, 2)) + axs[0].set_xlabel('a' * 30) + axs[1].set_xlabel('b' * 30) + with warnings.catch_warnings(record=True) as w: + fig.tight_layout() + assert len(w) == 1 + + +def test_big_decorators_vertical(): + "Test that warning emitted when xlabel too big" + fig, axs = plt.subplots(2, 1, figsize=(3, 2)) + axs[0].set_ylabel('a' * 20) + axs[1].set_ylabel('b' * 20) + with warnings.catch_warnings(record=True) as w: + fig.tight_layout() + assert len(w) == 1 diff --git a/lib/matplotlib/tight_layout.py b/lib/matplotlib/tight_layout.py index 21823f9aea38..1c18dc63c30d 100644 --- a/lib/matplotlib/tight_layout.py +++ b/lib/matplotlib/tight_layout.py @@ -171,25 +171,46 @@ def auto_adjust_subplotpars( margin_bottom = max([sum(s) for s in vspaces[-cols:]] + [0]) margin_bottom += pad_inches / fig_height_inch + if margin_left + margin_right >= 1: + margin_left = 0.4999 + margin_right = 0.4999 + warnings.warn('The left and right margins cannot be made large ' + 'enough to accommodate all axes decorations. ') + if margin_bottom + margin_top >= 1: + margin_bottom = 0.4999 + margin_top = 0.4999 + warnings.warn('The bottom and top margins cannot be made large ' + 'enough to accommodate all axes decorations. ') + kwargs = dict(left=margin_left, right=1 - margin_right, bottom=margin_bottom, top=1 - margin_top) - if cols > 1: hspace = ( max(sum(s) for i in range(rows) for s in hspaces[i * (cols + 1) + 1:(i + 1) * (cols + 1) - 1]) + hpad_inches / fig_width_inch) + # axes widths: h_axes = (1 - margin_right - margin_left - hspace * (cols - 1)) / cols - kwargs["wspace"] = hspace / h_axes + if h_axes < 0: + warnings.warn('tight_layout cannot make axes width small enough ' + 'to accommodate all axes decorations') + kwargs["wspace"] = 0.5 + else: + kwargs["wspace"] = hspace / h_axes if rows > 1: vspace = (max(sum(s) for s in vspaces[cols:-cols]) + vpad_inches / fig_height_inch) v_axes = (1 - margin_top - margin_bottom - vspace * (rows - 1)) / rows - kwargs["hspace"] = vspace / v_axes + if v_axes < 0: + warnings.warn('tight_layout cannot make axes height small enough ' + 'to accommodate all axes decorations') + kwargs["hspace"] = 0.5 + else: + kwargs["hspace"] = vspace / v_axes return kwargs