@@ -521,3 +521,37 @@ def test_bboxtight():
521521def test_bbox ():
522522 fig , ax = plt .subplots (constrained_layout = True )
523523 ax .set_aspect (1. )
524+
525+
526+ def test_align_labels ():
527+ """
528+ Tests for a bug in which constrained layout and align_ylabels on
529+ three unevenly sized subplots, one of whose y tick labels include
530+ negative numbers, drives the non-negative subplots' y labels off
531+ the edge of the plot
532+ """
533+ fig , (ax3 , ax1 , ax2 ) = plt .subplots (3 , 1 , constrained_layout = True ,
534+ figsize = (6.4 , 8 ),
535+ gridspec_kw = {"height_ratios" : (1 , 1 ,
536+ 0.7 )})
537+
538+ ax1 .set_ylim (0 , 1 )
539+ ax1 .set_ylabel ("Label" )
540+
541+ ax2 .set_ylim (- 1.5 , 1.5 )
542+ ax2 .set_ylabel ("Label" )
543+
544+ ax3 .set_ylim (0 , 1 )
545+ ax3 .set_ylabel ("Label" )
546+
547+ fig .align_ylabels (axs = (ax3 , ax1 , ax2 ))
548+
549+ fig .canvas .draw ()
550+ after_align = [ax1 .yaxis .label .get_window_extent (),
551+ ax2 .yaxis .label .get_window_extent (),
552+ ax3 .yaxis .label .get_window_extent ()]
553+ # ensure labels are approximately aligned
554+ np .testing .assert_allclose ([after_align [0 ].x0 , after_align [2 ].x0 ],
555+ after_align [1 ].x0 , rtol = 0 , atol = 1e-05 )
556+ # ensure labels do not go off the edge
557+ assert after_align [0 ].x0 >= 1
0 commit comments