Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit cf526b1

Browse filesBrowse files
authored
Merge pull request matplotlib#11598 from matplotlib/auto-backport-of-pr-10915-on-v2.2.x
Backport PR matplotlib#10915 on branch v2.2.x
2 parents 5b1a052 + 6fe8965 commit cf526b1
Copy full SHA for cf526b1

File tree

Expand file treeCollapse file tree

2 files changed

+62
-3
lines changed
Filter options
Expand file treeCollapse file tree

2 files changed

+62
-3
lines changed

‎lib/matplotlib/tests/test_tightlayout.py

Copy file name to clipboardExpand all lines: lib/matplotlib/tests/test_tightlayout.py
+38Lines 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_verybig_decorators_horizontal():
278+
"Test that warning emitted 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_verybig_decorators_vertical():
287+
"Test that warning emitted 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 emitted 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 emitted 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

Copy file name to clipboardExpand all lines: lib/matplotlib/tight_layout.py
+24-3Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -171,25 +171,46 @@ def auto_adjust_subplotpars(
171171
margin_bottom = max([sum(s) for s in vspaces[-cols:]] + [0])
172172
margin_bottom += pad_inches / fig_height_inch
173173

174+
if margin_left + margin_right >= 1:
175+
margin_left = 0.4999
176+
margin_right = 0.4999
177+
warnings.warn('The left and right margins cannot be made large '
178+
'enough to accommodate 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 accommodate all axes decorations. ')
184+
174185
kwargs = dict(left=margin_left,
175186
right=1 - margin_right,
176187
bottom=margin_bottom,
177188
top=1 - margin_top)
178-
179189
if cols > 1:
180190
hspace = (
181191
max(sum(s)
182192
for i in range(rows)
183193
for s in hspaces[i * (cols + 1) + 1:(i + 1) * (cols + 1) - 1])
184194
+ hpad_inches / fig_width_inch)
195+
# axes widths:
185196
h_axes = (1 - margin_right - margin_left - hspace * (cols - 1)) / cols
186-
kwargs["wspace"] = hspace / h_axes
197+
if h_axes < 0:
198+
warnings.warn('tight_layout cannot make axes width small enough '
199+
'to accommodate all axes decorations')
200+
kwargs["wspace"] = 0.5
201+
else:
202+
kwargs["wspace"] = hspace / h_axes
187203

188204
if rows > 1:
189205
vspace = (max(sum(s) for s in vspaces[cols:-cols])
190206
+ vpad_inches / fig_height_inch)
191207
v_axes = (1 - margin_top - margin_bottom - vspace * (rows - 1)) / rows
192-
kwargs["hspace"] = vspace / v_axes
208+
if v_axes < 0:
209+
warnings.warn('tight_layout cannot make axes height small enough '
210+
'to accommodate all axes decorations')
211+
kwargs["hspace"] = 0.5
212+
else:
213+
kwargs["hspace"] = vspace / v_axes
193214

194215
return kwargs
195216

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.