Skip to content

Commit 4730719

Browse files
committed
TST: tight_layout having negative width axes
1 parent 491c388 commit 4730719

File tree

2 files changed

+45
-1
lines changed

2 files changed

+45
-1
lines changed

lib/matplotlib/tests/test_tightlayout.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,3 +272,41 @@ def test_empty_layout():
272272

273273
fig = plt.gcf()
274274
fig.tight_layout()
275+
276+
277+
def test_big_decorators_horizontal():
278+
"Test that warning emited when xlabel too big"
279+
fig, ax = plt.subplots(figsize=(3, 2))
280+
ax.set_xlabel('a' * 100)
281+
with warnings.catch_warnings(record=True) as w:
282+
fig.tight_layout()
283+
assert len(w) == 1
284+
285+
286+
def test_big_decorators_verticaltal():
287+
"Test that warning emited when xlabel too big"
288+
fig, ax = plt.subplots(figsize=(3, 2))
289+
ax.set_ylabel('a' * 100)
290+
with warnings.catch_warnings(record=True) as w:
291+
fig.tight_layout()
292+
assert len(w) == 1
293+
294+
295+
def test_big_decorators_horizontal():
296+
"Test that warning emited when xlabel too big"
297+
fig, axs = plt.subplots(1, 2, figsize=(3, 2))
298+
axs[0].set_xlabel('a' * 30)
299+
axs[1].set_xlabel('b' * 30)
300+
with warnings.catch_warnings(record=True) as w:
301+
fig.tight_layout()
302+
assert len(w) == 1
303+
304+
305+
def test_big_decorators_vertical():
306+
"Test that warning emited when xlabel too big"
307+
fig, axs = plt.subplots(2, 1, figsize=(3, 2))
308+
axs[0].set_ylabel('a' * 20)
309+
axs[1].set_ylabel('b' * 20)
310+
with warnings.catch_warnings(record=True) as w:
311+
fig.tight_layout()
312+
assert len(w) == 1

lib/matplotlib/tight_layout.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,12 @@ def auto_adjust_subplotpars(
176176
margin_right = 0.4999
177177
warnings.warn('The left and right margins cannot be made large '
178178
'enough to accomodate all axes decorations. ')
179+
if margin_bottom + margin_top >= 1:
180+
margin_bottom = 0.4999
181+
margin_top = 0.4999
182+
warnings.warn('The bottom and top margins cannot be made large '
183+
'enough to accomodate all axes decorations. ')
184+
179185
kwargs = dict(left=margin_left,
180186
right=1 - margin_right,
181187
bottom=margin_bottom,
@@ -188,7 +194,7 @@ def auto_adjust_subplotpars(
188194
+ hpad_inches / fig_width_inch)
189195
# axes widths:
190196
h_axes = (1 - margin_right - margin_left - hspace * (cols - 1)) / cols
191-
if h_axes < 0.:
197+
if h_axes < 0:
192198
warnings.warn('tight_layout cannot make axes width small enough '
193199
'to accomodate all axes decorations')
194200
kwargs["wspace"] = 0.5

0 commit comments

Comments
 (0)