Skip to content

FIX: tight_layout having negative width axes #10915

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 7, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions lib/matplotlib/tests/test_tightlayout.py
Original file line number Diff line number Diff line change
Expand Up @@ -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():
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

codecov drop is because this test name is the same.

"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():
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably the same name as before too if you fix the other's typo.

"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
27 changes: 24 additions & 3 deletions lib/matplotlib/tight_layout.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down