Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Backport PR #20761 on branch v3.4.x (Fix suplabel autopos) #20763

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
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
5 changes: 4 additions & 1 deletion lib/matplotlib/figure.py
Original file line number Diff line number Diff line change
Expand Up @@ -373,7 +373,10 @@ def _suplabels(self, t, info, **kwargs):

x = kwargs.pop('x', None)
y = kwargs.pop('y', None)
autopos = x is None and y is None
if info['name'] in ['_supxlabel', '_suptitle']:
autopos = y is None
elif info['name'] == '_supylabel':
autopos = x is None
if x is None:
x = info['x0']
if y is None:
Expand Down
23 changes: 23 additions & 0 deletions lib/matplotlib/tests/test_constrainedlayout.py
Original file line number Diff line number Diff line change
Expand Up @@ -555,3 +555,26 @@ def test_align_labels():
after_align[1].x0, rtol=0, atol=1e-05)
# ensure labels do not go off the edge
assert after_align[0].x0 >= 1


def test_suplabels():
fig, ax = plt.subplots(constrained_layout=True)
fig.canvas.draw()
pos0 = ax.get_tightbbox(fig.canvas.get_renderer())
fig.supxlabel('Boo')
fig.supylabel('Booy')
fig.canvas.draw()
pos = ax.get_tightbbox(fig.canvas.get_renderer())
assert pos.y0 > pos0.y0 + 10.0
assert pos.x0 > pos0.x0 + 10.0

fig, ax = plt.subplots(constrained_layout=True)
fig.canvas.draw()
pos0 = ax.get_tightbbox(fig.canvas.get_renderer())
# check that specifying x (y) doesn't ruin the layout
fig.supxlabel('Boo', x=0.5)
fig.supylabel('Boo', y=0.5)
fig.canvas.draw()
pos = ax.get_tightbbox(fig.canvas.get_renderer())
assert pos.y0 > pos0.y0 + 10.0
assert pos.x0 > pos0.x0 + 10.0