@@ -398,12 +398,18 @@ def make_layout_margins(layoutgrids, fig, renderer, *, w_pad=0, h_pad=0,
398398 # make margin for colorbars. These margins go in the
399399 # padding margin, versus the margin for Axes decorators.
400400 for cbax in ax ._colorbars :
401+ if cbax ._colorbar_info ["type" ] == "MultivarColorbar" :
402+ # a matplotlib.colorbar.MultivarColorbar object
403+ fig = cbax .axes [0 ].get_figure (root = False )
404+ tightbbox = cbax .get_tightbbox (renderer , for_layout_only = True )
405+ cbbbox = tightbbox .transformed (fig .transFigure .inverted ())
406+ else :
407+ cbpos , cbbbox = get_pos_and_bbox (cbax , renderer )
401408 # note pad is a fraction of the parent width...
402409 pad = colorbar_get_pad (layoutgrids , cbax )
403410 # colorbars can be child of more than one subplot spec:
404411 cbp_rspan , cbp_cspan = get_cb_parent_spans (cbax )
405412 loc = cbax ._colorbar_info ['location' ]
406- cbpos , cbbbox = get_pos_and_bbox (cbax , renderer )
407413 if loc == 'right' :
408414 if cbp_cspan .stop == ss .colspan .stop :
409415 # only increase if the colorbar is on the right edge
@@ -700,7 +706,7 @@ def reposition_colorbar(layoutgrids, cbax, renderer, *, offset=None, compress=Fa
700706 Parameters
701707 ----------
702708 layoutgrids : dict
703- cbax : `~matplotlib.axes.Axes`
709+ cbax : `~matplotlib.colorbar.MultiColorbars` or `~matplotlib. axes.Axes`
704710 Axes for the colorbar.
705711 renderer : `~matplotlib.backend_bases.RendererBase` subclass.
706712 The renderer to use.
@@ -710,22 +716,30 @@ def reposition_colorbar(layoutgrids, cbax, renderer, *, offset=None, compress=Fa
710716 compress : bool
711717 Whether we're in compressed layout mode.
712718 """
719+ cb_info = cbax ._colorbar_info
720+ if cb_info ["type" ] == "MultivarColorbar" :
721+ multi_cbar = cbax
722+ cbaxes = multi_cbar .axes
723+ multi = True
724+ else :
725+ multi = False
726+ cbaxes = [cbax ]
713727
714- parents = cbax . _colorbar_info ['parents' ]
728+ parents = cb_info ['parents' ]
715729 gs = parents [0 ].get_gridspec ()
716- fig = cbax .get_figure (root = False )
730+ fig = cbaxes [ 0 ] .get_figure (root = False )
717731 trans_fig_to_subfig = fig .transFigure - fig .transSubfigure
718732
719733 cb_rspans , cb_cspans = get_cb_parent_spans (cbax )
720734 bboxparent = layoutgrids [gs ].get_bbox_for_cb (rows = cb_rspans ,
721735 cols = cb_cspans )
722736 pb = layoutgrids [gs ].get_inner_bbox (rows = cb_rspans , cols = cb_cspans )
723737
724- location = cbax . _colorbar_info ['location' ]
725- anchor = cbax . _colorbar_info ['anchor' ]
726- fraction = cbax . _colorbar_info ['fraction' ]
727- aspect = cbax . _colorbar_info ['aspect' ]
728- shrink = cbax . _colorbar_info ['shrink' ]
738+ location = cb_info ['location' ]
739+ anchor = cb_info ['anchor' ]
740+ fraction = cb_info ['fraction' ]
741+ aspect = cb_info ['aspect' ]
742+ shrink = cb_info ['shrink' ]
729743
730744 # For colorbars with a single parent in compressed layout,
731745 # use the actual visual size of the parent axis after apply_aspect()
@@ -752,7 +766,13 @@ def reposition_colorbar(layoutgrids, cbax, renderer, *, offset=None, compress=Fa
752766 pb = Bbox .from_extents (actual_pos_fig .x0 , pb .y0 ,
753767 actual_pos_fig .x1 , pb .y1 )
754768
755- cbpos , cbbbox = get_pos_and_bbox (cbax , renderer )
769+ if multi :
770+ tightbbox = martist ._get_tightbbox_for_layout_only (multi_cbar , renderer )
771+ cbbbox = tightbbox .transformed (fig .transFigure .inverted ())
772+ cbpos = multi_cbar ._get_original_position ()
773+ cbpos = cbpos .transformed (fig .transSubfigure - fig .transFigure )
774+ else :
775+ cbpos , cbbbox = get_pos_and_bbox (cbax , renderer )
756776
757777 # Colorbar gets put at extreme edge of outer bbox of the subplotspec
758778 # It needs to be moved in by: 1) a pad 2) its "margin" 3) by
@@ -763,6 +783,8 @@ def reposition_colorbar(layoutgrids, cbax, renderer, *, offset=None, compress=Fa
763783 pbcb = pb .shrunk (fraction , shrink ).anchored (anchor , pb )
764784 # The colorbar is at the left side of the parent. Need
765785 # to translate to right (or left)
786+ if multi :
787+ pbcb .x1 = pbcb .x0 + cbbbox .width
766788 if location == 'right' :
767789 lmargin = cbpos .x0 - cbbbox .x0
768790 dx = bboxparent .x1 - pbcb .x0 + offset ['right' ]
@@ -777,6 +799,8 @@ def reposition_colorbar(layoutgrids, cbax, renderer, *, offset=None, compress=Fa
777799 pbcb = pbcb .translated (dx , 0 )
778800 else : # horizontal axes:
779801 pbcb = pb .shrunk (shrink , fraction ).anchored (anchor , pb )
802+ if multi :
803+ pbcb .y1 = pbcb .y0 + cbbbox .height
780804 if location == 'top' :
781805 bmargin = cbpos .y0 - cbbbox .y0
782806 dy = bboxparent .y1 - pbcb .y0 + offset ['top' ]
@@ -790,14 +814,19 @@ def reposition_colorbar(layoutgrids, cbax, renderer, *, offset=None, compress=Fa
790814 offset ['bottom' ] += cbbbox .height + cbpad
791815 pbcb = pbcb .translated (0 , dy )
792816
793- pbcb = trans_fig_to_subfig .transform_bbox (pbcb )
794- cbax .set_transform (fig .transSubfigure )
795- cbax ._set_position (pbcb )
796- cbax .set_anchor (anchor )
797817 if location in ['bottom' , 'top' ]:
798818 aspect = 1 / aspect
799- cbax .set_box_aspect (aspect )
800- cbax .set_aspect ('auto' )
819+ if multi :
820+ new_bboxs = multi_cbar ._get_tight_packing_inside (pbcb )
821+ else :
822+ new_bboxs = [pbcb ]
823+ for cbax , new_bbox in zip (cbaxes , new_bboxs ):
824+ transformed_bbox = trans_fig_to_subfig .transform_bbox (new_bbox )
825+ cbax .set_transform (fig .transSubfigure )
826+ cbax ._set_position (transformed_bbox )
827+ cbax .set_anchor (anchor )
828+ cbax .set_box_aspect (aspect )
829+ cbax .set_aspect ('auto' )
801830 return offset
802831
803832
0 commit comments