@@ -623,3 +623,85 @@ def test_colorbar_int(clim):
623623 im = ax .imshow ([[* map (np .int16 , clim )]])
624624 fig .colorbar (im )
625625 assert (im .norm .vmin , im .norm .vmax ) == clim
626+
627+
628+ def test_anchored_cbar_position_using_specgrid ():
629+ data = np .arange (1200 ).reshape (30 , 40 )
630+ levels = [0 , 200 , 400 , 600 , 800 , 1000 , 1200 ]
631+ shrink = 0.5
632+ anchor_y = 0.3
633+ # right
634+ fig , ax = plt .subplots ()
635+ cs = ax .contourf (data , levels = levels )
636+ cbar = plt .colorbar (
637+ cs , ax = ax , use_gridspec = True ,
638+ location = 'right' , anchor = (1 , anchor_y ), shrink = shrink )
639+
640+ # the bottom left corner of one ax is (x0, y0)
641+ # the top right corner of one ax is (x1, y1)
642+ # p0: the vertical / horizontal position of anchor
643+ x0 , y0 , x1 , y1 = ax .get_position ().extents
644+ cx0 , cy0 , cx1 , cy1 = cbar .ax .get_position ().extents
645+ p0 = (y1 - y0 ) * anchor_y + y0
646+
647+ np .testing .assert_allclose (
648+ [cy1 , cy0 ],
649+ [y1 * shrink + (1 - shrink ) * p0 , p0 * (1 - shrink ) + y0 * shrink ])
650+
651+ # left
652+ fig , ax = plt .subplots ()
653+ cs = ax .contourf (data , levels = levels )
654+ cbar = plt .colorbar (
655+ cs , ax = ax , use_gridspec = True ,
656+ location = 'left' , anchor = (1 , anchor_y ), shrink = shrink )
657+
658+ # the bottom left corner of one ax is (x0, y0)
659+ # the top right corner of one ax is (x1, y1)
660+ # p0: the vertical / horizontal position of anchor
661+ x0 , y0 , x1 , y1 = ax .get_position ().extents
662+ cx0 , cy0 , cx1 , cy1 = cbar .ax .get_position ().extents
663+ p0 = (y1 - y0 ) * anchor_y + y0
664+
665+ np .testing .assert_allclose (
666+ [cy1 , cy0 ],
667+ [y1 * shrink + (1 - shrink ) * p0 , p0 * (1 - shrink ) + y0 * shrink ])
668+
669+ # top
670+ shrink = 0.5
671+ anchor_x = 0.3
672+ fig , ax = plt .subplots ()
673+ cs = ax .contourf (data , levels = levels )
674+ cbar = plt .colorbar (
675+ cs , ax = ax , use_gridspec = True ,
676+ location = 'top' , anchor = (anchor_x , 1 ), shrink = shrink )
677+
678+ # the bottom left corner of one ax is (x0, y0)
679+ # the top right corner of one ax is (x1, y1)
680+ # p0: the vertical / horizontal position of anchor
681+ x0 , y0 , x1 , y1 = ax .get_position ().extents
682+ cx0 , cy0 , cx1 , cy1 = cbar .ax .get_position ().extents
683+ p0 = (x1 - x0 ) * anchor_x + x0
684+
685+ np .testing .assert_allclose (
686+ [cx1 , cx0 ],
687+ [x1 * shrink + (1 - shrink ) * p0 , p0 * (1 - shrink ) + x0 * shrink ])
688+
689+ # bottom
690+ shrink = 0.5
691+ anchor_x = 0.3
692+ fig , ax = plt .subplots ()
693+ cs = ax .contourf (data , levels = levels )
694+ cbar = plt .colorbar (
695+ cs , ax = ax , use_gridspec = True ,
696+ location = 'bottom' , anchor = (anchor_x , 1 ), shrink = shrink )
697+
698+ # the bottom left corner of one ax is (x0, y0)
699+ # the top right corner of one ax is (x1, y1)
700+ # p0: the vertical / horizontal position of anchor
701+ x0 , y0 , x1 , y1 = ax .get_position ().extents
702+ cx0 , cy0 , cx1 , cy1 = cbar .ax .get_position ().extents
703+ p0 = (x1 - x0 ) * anchor_x + x0
704+
705+ np .testing .assert_allclose (
706+ [cx1 , cx0 ],
707+ [x1 * shrink + (1 - shrink ) * p0 , p0 * (1 - shrink ) + x0 * shrink ])
0 commit comments