Skip to content

Fix #5456: Keep margins <= .5 in tight_layout #6161

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

Closed
wants to merge 4 commits into from
Closed
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
17 changes: 16 additions & 1 deletion lib/matplotlib/tests/test_tightlayout.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
from __future__ import (absolute_import, division, print_function,
unicode_literals)

import matplotlib
from matplotlib.externals import six
import warnings

import numpy as np

from matplotlib.testing.decorators import image_comparison, knownfailureif
from matplotlib.testing.decorators import (image_comparison, knownfailureif,
cleanup)
import matplotlib.pyplot as plt
from nose.tools import assert_raises
from numpy.testing import assert_array_equal
Expand Down Expand Up @@ -253,3 +255,16 @@ def _subplots():
child.set_visible(False)

plt.tight_layout()


@cleanup
def test_long_label():
'''
Test for 5456. No ValueError when using very long label.
'''
try:
fig, ax = plt.subplots()
ax.set_ylabel('a'*50000)
plt.tight_layout()
except ValueError:
assert False, "ValueError raised from long label"
6 changes: 6 additions & 0 deletions lib/matplotlib/tight_layout.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,22 +165,28 @@ def auto_adjust_subplotpars(fig, renderer,

# margins can be negative for axes with aspect applied. And we
# append + [0] to make minimum margins 0
# We must also ensure that maximum margins are .5 so that we do not
# end up with bottom >= top or left >= right

if not margin_left:
margin_left = max([sum(s) for s in hspaces[::cols + 1]] + [0])
margin_left += pad_inches / fig_width_inch
margin_left = min(0.5, margin_left)

if not margin_right:
margin_right = max([sum(s) for s in hspaces[cols::cols + 1]] + [0])
margin_right += pad_inches / fig_width_inch
margin_right = min(0.499, margin_right)

if not margin_top:
margin_top = max([sum(s) for s in vspaces[:cols]] + [0])
margin_top += pad_inches / fig_height_inch
margin_top = min(0.499, margin_top)

if not margin_bottom:
margin_bottom = max([sum(s) for s in vspaces[-cols:]] + [0])
margin_bottom += pad_inches / fig_height_inch
margin_bottom = min(0.5, margin_bottom)

kwargs = dict(left=margin_left,
right=1 - margin_right,
Expand Down