Skip to content

Commit 1685bcb

Browse files
authored
Merge pull request #20761 from jklymak/fix-suplabel-autopos
Fix suplabel autopos
2 parents a254f83 + e022c04 commit 1685bcb

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
@@ -370,7 +370,10 @@ def _suplabels(self, t, info, **kwargs):
370370

371371
x = kwargs.pop('x', None)
372372
y = kwargs.pop('y', None)
373-
autopos = x is None and y is None
373+
if info['name'] in ['_supxlabel', '_suptitle']:
374+
autopos = y is None
375+
elif info['name'] == '_supylabel':
376+
autopos = x is None
374377
if x is None:
375378
x = info['x0']
376379
if y is None:

lib/matplotlib/tests/test_constrainedlayout.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -537,3 +537,26 @@ def test_align_labels():
537537
after_align[1].x0, rtol=0, atol=1e-05)
538538
# ensure labels do not go off the edge
539539
assert after_align[0].x0 >= 1
540+
541+
542+
def test_suplabels():
543+
fig, ax = plt.subplots(constrained_layout=True)
544+
fig.draw_no_output()
545+
pos0 = ax.get_tightbbox(fig.canvas.get_renderer())
546+
fig.supxlabel('Boo')
547+
fig.supylabel('Booy')
548+
fig.draw_no_output()
549+
pos = ax.get_tightbbox(fig.canvas.get_renderer())
550+
assert pos.y0 > pos0.y0 + 10.0
551+
assert pos.x0 > pos0.x0 + 10.0
552+
553+
fig, ax = plt.subplots(constrained_layout=True)
554+
fig.draw_no_output()
555+
pos0 = ax.get_tightbbox(fig.canvas.get_renderer())
556+
# check that specifying x (y) doesn't ruin the layout
557+
fig.supxlabel('Boo', x=0.5)
558+
fig.supylabel('Boo', y=0.5)
559+
fig.draw_no_output()
560+
pos = ax.get_tightbbox(fig.canvas.get_renderer())
561+
assert pos.y0 > pos0.y0 + 10.0
562+
assert pos.x0 > pos0.x0 + 10.0

0 commit comments

Comments
 (0)