8000 Backport PR #20761: Fix suplabel autopos · matplotlib/matplotlib@5b64248 · GitHub
[go: up one dir, main page]

Skip to content

Commit 5b64248

Browse files
committed
Backport PR #20761: Fix suplabel autopos
1 parent 30b9e16 commit 5b64248

File tree

2 files changed

+27
-1
lines changed

2 files changed

+27
-1
lines changed

lib/matplotlib/figure.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -373,7 +373,10 @@ def _suplabels(self, t, info, **kwargs):
373373

374374
x = kwargs.pop('x', None)
375375
y = kwargs.pop('y', None)
376-
autopos = x is None and y is None
376+
if info['name'] in ['_supxlabel', '_suptitle']:
377+
autopos = y is None
378+
elif info['name'] == '_supylabel':
379+
autopos = x is None
377380
if x is None 8000 :
378381
x = info['x0']
379382
if y is None:

lib/matplotlib/tests/test_constrainedlayout.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -555,3 +555,26 @@ def test_align_labels():
555555
after_align[1].x0, rtol=0, atol=1e-05)
556556
# ensure labels do not go off the edge
557557
assert after_align[0].x0 >= 1
558+
559+
560+
def test_suplabels():
561+
fig, ax = plt.subplots(constrained_layout=True)
562+
fig.canvas.draw()
563+
pos0 = ax.get_tightbbox(fig.canvas.get_renderer())
564+
fig.supxlabel('Boo')
565+
fig.supylabel('Booy')
566+
fig.canvas.draw()
567+
pos = ax.get_tightbbox(fig.canvas.get_renderer())
568+
assert pos.y0 > pos0.y0 + 10.0
569+
assert pos.x0 > pos0.x0 + 10.0
570+
571+
fig, ax = plt.subplots(constrained_layout=True)
572+
fig.canvas.draw()
573+
pos0 = ax.get_tightbbox(fig.canvas.get_renderer())
574+
# check that specifying x (y) doesn't ruin the layout
575+
fig.supxlabel('Boo', x=0.5)
576+
fig.supylabel('Boo', y=0.5)
577+
fig.canvas.draw()
578+
pos = ax.get_tightbbox(fig.canvas.get_renderer())
579+
assert pos.y0 > pos0.y0 + 10.0
580+
assert pos.x0 > pos0.x0 + 10.0

0 commit comments

Comments
 (0)
0