@@ -572,6 +572,36 @@ def layoutcolorbarsingle(ax, cax, shrink, aspect, location, pad=0.05):
572572 return lb , lbpos
573573
574574
575+ def _getmaxminrowcolumn (axs ):
576+ # helper to get the min/max rows and columns of a list of axes.
577+ maxrow = - 100000
578+ minrow = 1000000
579+ maxax = None
580+ minax = None
581+ maxcol = - 100000
582+ mincol = 1000000
583+ maxax_col = None
584+ minax_col = None
585+
586+ for ax in axs :
587+ subspec = ax .get_subplotspec ()
588+ nrows , ncols , row_start , row_stop , col_start , col_stop = \
589+ subspec .get_rows_columns ()
590+ if row_stop > maxrow :
591+ maxrow = row_stop
592+ maxax = ax
593+ if row_start < minrow :
594+ minrow = row_start
595+ minax = ax
596+ if col_stop > maxcol :
597+ maxcol = col_stop
598+ maxax_col = ax
599+ if col_start < mincol :
600+ mincol = col_start
601+ minax_col = ax
602+ return (minrow , maxrow , minax , maxax , mincol , maxcol , minax_col , maxax_col )
603+
604+
575605def layoutcolorbargridspec (parents , cax , shrink , aspect , location , pad = 0.05 ):
576606 """
577607 Do the layout for a colorbar, to not oeverly pollute colorbar.py
@@ -586,6 +616,10 @@ def layoutcolorbargridspec(parents, cax, shrink, aspect, location, pad=0.05):
586616 lb = layoutbox .LayoutBox (parent = gslb .parent ,
587617 name = gslb .parent .name + '.cbar' ,
588618 artist = cax )
619+ # figure out the row and column extent of the parents.
620+ (minrow , maxrow , minax_row , maxax_row ,
621+ mincol , maxcol , minax_col , maxax_col ) = _getmaxminrowcolumn (parents )
622+
589623 if location in ('left' , 'right' ):
590624 lbpos = layoutbox .LayoutBox (
591625 parent = lb ,
@@ -594,39 +628,43 @@ def layoutcolorbargridspec(parents, cax, shrink, aspect, location, pad=0.05):
594628 pos = True ,
595629 subplot = False ,
596630 artist = cax )
597-
598- if location == 'right' :
599- # arrange to right of the gridpec sibbling
600- layoutbox . hstack ([ gslb , lb ], padding = pad * gslb . width ,
601- strength = 'strong' )
602- else :
603- layoutbox . hstack ([ lb , gslb ], padding = pad * gslb . width )
631+ for ax in parents :
632+ if location == 'right' :
633+ order = [ ax . _layoutbox , lb ]
634+ else :
635+ order = [ lb , ax . _layoutbox ]
636+ layoutbox . hstack ( order , padding = pad * gslb . width ,
637+ strength = 'strong' )
604638 # constrain the height and center...
605639 # This isn't quite right. We'd like the colorbar
606640 # pos to line up w/ the axes poss, not the size of the
607641 # gs.
608- maxrow = - 100000
609- minrow = 1000000
610- maxax = None
611- minax = None
612642
613- for ax in parents :
614- subspec = ax .get_subplotspec ()
615- nrows , ncols = subspec .get_gridspec ().get_geometry ()
616- for num in [subspec .num1 , subspec .num2 ]:
617- rownum1 , colnum1 = divmod (subspec .num1 , ncols )
618- if rownum1 > maxrow :
619- maxrow = rownum1
620- maxax = ax
621- if rownum1 < minrow :
622- minrow = rownum1
623- minax = ax
624- # invert the order so these are bottom to top:
625- maxposlb = minax ._poslayoutbox
626- minposlb = maxax ._poslayoutbox
643+ # Horizontal Layout: need to check all the axes in this gridspec
644+ for ch in gslb .children :
645+ subspec = ch .artist
646+ nrows , ncols , row_start , row_stop , col_start , col_stop = \
647+ subspec .get_rows_columns ()
648+ if location == 'right' :
649+ if col_stop <= maxcol :
650+ order = [subspec ._layoutbox , lb ]
651+ # arrange to right of the parents
652+ if col_start > maxcol :
653+ order = [lb , subspec ._layoutbox ]
654+ elif location == 'left' :
655+ if col_start >= mincol :
656+ order = [lb , subspec ._layoutbox ]
657+ if col_stop < mincol :
658+ order = [subspec ._layoutbox , lb ]
659+ layoutbox .hstack (order , padding = pad * gslb .width ,
660+ strength = 'strong' )
661+
662+ # Vertical layout:
663+ maxposlb = minax_row ._poslayoutbox
664+ minposlb = maxax_row ._poslayoutbox
627665 # now we want the height of the colorbar pos to be
628- # set by the top and bottom of these poss
629- # bottom top
666+ # set by the top and bottom of the min/max axes...
667+ # bottom top
630668 # b t
631669 # h = (top-bottom)*shrink
632670 # b = bottom + (top-bottom - h) / 2.
@@ -650,29 +688,35 @@ def layoutcolorbargridspec(parents, cax, shrink, aspect, location, pad=0.05):
650688 subplot = False ,
651689 artist = cax )
652690
653- if location == 'bottom' :
654- layoutbox .vstack ([gslb , lb ], padding = pad * gslb .width )
655- else :
656- layoutbox .vstack ([lb , gslb ], padding = pad * gslb .width )
657-
658- maxcol = - 100000
659- mincol = 1000000
660- maxax = None
661- minax = None
662-
663691 for ax in parents :
664- subspec = ax .get_subplotspec ()
665- nrows , ncols = subspec .get_gridspec ().get_geometry ()
666- for num in [subspec .num1 , subspec .num2 ]:
667- rownum1 , colnum1 = divmod (subspec .num1 , ncols )
668- if colnum1 > maxcol :
669- maxcol = colnum1
670- maxax = ax
671- if rownum1 < mincol :
672- mincol = colnum1
673- minax = ax
674- maxposlb = maxax ._poslayoutbox
675- minposlb = minax ._poslayoutbox
692+ if location == 'bottom' :
693+ order = [ax ._layoutbox , lb ]
694+ else :
695+ order = [lb , ax ._layoutbox ]
696+ layoutbox .vstack (order , padding = pad * gslb .width ,
697+ strength = 'strong' )
698+
699+ # Vertical Layout: need to check all the axes in this gridspec
700+ for ch in gslb .children :
701+ subspec = ch .artist
702+ nrows , ncols , row_start , row_stop , col_start , col_stop = \
703+ subspec .get_rows_columns ()
704+ if location == 'bottom' :
705+ if row_stop <= minrow :
706+ order = [subspec ._layoutbox , lb ]
707+ if row_start > maxrow :
708+ order = [lb , subspec ._layoutbox ]
709+ elif location == 'top' :
710+ if row_stop < minrow :
711+ order = [subspec ._layoutbox , lb ]
712+ if row_start >= maxrow :
713+ order = [lb , subspec ._layoutbox ]
714+ layoutbox .vstack (order , padding = pad * gslb .width ,
715+ strength = 'strong' )
716+
717+ # Do horizontal layout...
718+ maxposlb = maxax_col ._poslayoutbox
719+ minposlb = minax_col ._poslayoutbox
676720 lbpos .constrain_width ((maxposlb .right - minposlb .left ) *
677721 shrink )
678722 lbpos .constrain_left (
0 commit comments