From 491c388010f0281b7fab8751bd5d72f8e38e2d74 Mon Sep 17 00:00:00 2001 From: Jody Klymak Date: Wed, 28 Mar 2018 20:55:48 -0700 Subject: [PATCH 1/2] FIX: tight_layout having negative width axes --- lib/matplotlib/tight_layout.py | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/lib/matplotlib/tight_layout.py b/lib/matplotlib/tight_layout.py index 1c4ea66e5c9b..f82ec5187df7 100644 --- a/lib/matplotlib/tight_layout.py +++ b/lib/matplotlib/tight_layout.py @@ -171,25 +171,40 @@ 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 accomodate 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 accomodate 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 accomodate all axes decorations') + kwargs["hspace"] = 0.5 + else: + kwargs["hspace"] = vspace / v_axes return kwargs From abf260f930b9f20578175bace0aeeb1a77b4dd68 Mon Sep 17 00:00:00 2001 From: Jody Klymak Date: Thu, 29 Mar 2018 07:05:09 -0700 Subject: [PATCH 2/2] TST: tight_layout having negative width axes --- lib/matplotlib/tests/test_tightlayout.py | 38 ++++++++++++++++++++++++ lib/matplotlib/tight_layout.py | 14 ++++++--- 2 files changed, 48 insertions(+), 4 deletions(-) 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 f82ec5187df7..f5a05ce02b7e 100644 --- a/lib/matplotlib/tight_layout.py +++ b/lib/matplotlib/tight_layout.py @@ -175,7 +175,13 @@ def auto_adjust_subplotpars( margin_left = 0.4999 margin_right = 0.4999 warnings.warn('The left and right margins cannot be made large ' - 'enough to accomodate all axes decorations. ') + '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, @@ -188,9 +194,9 @@ def auto_adjust_subplotpars( + hpad_inches / fig_width_inch) # axes widths: h_axes = (1 - margin_right - margin_left - hspace * (cols - 1)) / cols - if h_axes < 0.: + if h_axes < 0: warnings.warn('tight_layout cannot make axes width small enough ' - 'to accomodate all axes decorations') + 'to accommodate all axes decorations') kwargs["wspace"] = 0.5 else: kwargs["wspace"] = hspace / h_axes @@ -201,7 +207,7 @@ def auto_adjust_subplotpars( v_axes = (1 - margin_top - margin_bottom - vspace * (rows - 1)) / rows if v_axes < 0: warnings.warn('tight_layout cannot make axes height small enough ' - 'to accomodate all axes decorations') + 'to accommodate all axes decorations') kwargs["hspace"] = 0.5 else: kwargs["hspace"] = vspace / v_axes