@@ -568,6 +568,36 @@ def layoutcolorbarsingle(ax, cax, shrink, aspect, location, pad=0.05):
568568 return lb , lbpos
569569
570570
571+ def _getmaxminrowcolumn (axs ):
572+ # helper to get the min/max rows and columns of a list of axes.
573+ maxrow = - 100000
574+ minrow = 1000000
575+ maxax = None
576+ minax = None
577+ maxcol = - 100000
578+ mincol = 1000000
579+ maxax_col = None
580+ minax_col = None
581+
582+ for ax in axs :
583+ subspec = ax .get_subplotspec ()
584+ nrows , ncols , row_start , row_stop , col_start , col_stop = \
585+ subspec .get_rows_columns ()
586+ if row_stop > maxrow :
587+ maxrow = row_stop
588+ maxax = ax
589+ if row_start < minrow :
590+ minrow = row_start
591+ minax = ax
592+ if col_stop > maxcol :
593+ maxcol = col_stop
594+ maxax_col = ax
595+ if col_start < mincol :
596+ mincol = col_start
597+ minax_col = ax
598+ return (minrow , maxrow , minax , maxax , mincol , maxcol , minax_col , maxax_col )
599+
600+
571601def layoutcolorbargridspec (parents , cax , shrink , aspect , location , pad = 0.05 ):
572602 """
573603 Do the layout for a colorbar, to not oeverly pollute colorbar.py
@@ -582,6 +612,10 @@ def layoutcolorbargridspec(parents, cax, shrink, aspect, location, pad=0.05):
582612 lb = layoutbox .LayoutBox (parent = gslb .parent ,
583613 name = gslb .parent .name + '.cbar' ,
584614 artist = cax )
615+ # figure out the row and column extent of the parents.
616+ (minrow , maxrow , minax_row , maxax_row ,
617+ mincol , maxcol , minax_col , maxax_col ) = _getmaxminrowcolumn (parents )
618+
585619 if location in ('left' , 'right' ):
586620 lbpos = layoutbox .LayoutBox (
587621 parent = lb ,
@@ -590,39 +624,43 @@ def layoutcolorbargridspec(parents, cax, shrink, aspect, location, pad=0.05):
590624 pos = True ,
591625 subplot = False ,
592626 artist = cax )
593-
594- if location == 'right' :
595- # arrange to right of the gridpec sibbling
596- layoutbox . hstack ([ gslb , lb ], padding = pad * gslb . width ,
597- strength = 'strong' )
598- else :
599- layoutbox . hstack ([ lb , gslb ], padding = pad * gslb . width )
627+ for ax in parents :
628+ if location == 'right' :
629+ order = [ ax . _layoutbox , lb ]
630+ else :
631+ order = [ lb , ax . _layoutbox ]
632+ layoutbox . hstack ( order , padding = pad * gslb . width ,
633+ strength = 'strong' )
600634 # constrain the height and center...
601635 # This isn't quite right. We'd like the colorbar
602636 # pos to line up w/ the axes poss, not the size of the
603637 # gs.
604- maxrow = - 100000
605- minrow = 1000000
606- maxax = None
607- minax = None
608638
609- for ax in parents :
610- subspec = ax .get_subplotspec ()
611- nrows , ncols = subspec .get_gridspec ().get_geometry ()
612- for num in [subspec .num1 , subspec .num2 ]:
613- rownum1 , colnum1 = divmod (subspec .num1 , ncols )
614- if rownum1 > maxrow :
615- maxrow = rownum1
616- maxax = ax
617- if rownum1 < minrow :
618- minrow = rownum1
619- minax = ax
620- # invert the order so these are bottom to top:
621- maxposlb = minax ._poslayoutbox
622- minposlb = maxax ._poslayoutbox
639+ # Horizontal Layout: need to check all the axes in this gridspec
640+ for ch in gslb .children :
641+ subspec = ch .artist
642+ nrows , ncols , row_start , row_stop , col_start , col_stop = \
643+ subspec .get_rows_columns ()
644+ if location == 'right' :
645+ if col_stop <= maxcol :
646+ order = [subspec ._layoutbox , lb ]
647+ # arrange to right of the parents
648+ if col_start > maxcol :
649+ order = [lb , subspec ._layoutbox ]
650+ elif location == 'left' :
651+ if col_start >= mincol :
652+ order = [lb , subspec ._layoutbox ]
653+ if col_stop < mincol :
654+ order = [subspec ._layoutbox , lb ]
655+ layoutbox .hstack (order , padding = pad * gslb .width ,
656+ strength = 'strong' )
657+
658+ # Vertical layout:
659+ maxposlb = minax_row ._poslayoutbox
660+ minposlb = maxax_row ._poslayoutbox
623661 # now we want the height of the colorbar pos to be
624- # set by the top and bottom of these poss
625- # bottom top
662+ # set by the top and bottom of the min/max axes...
663+ # bottom top
626664 # b t
627665 # h = (top-bottom)*shrink
628666 # b = bottom + (top-bottom - h) / 2.
@@ -646,29 +684,35 @@ def layoutcolorbargridspec(parents, cax, shrink, aspect, location, pad=0.05):
646684 subplot = False ,
647685 artist = cax )
648686
649- if location == 'bottom' :
650- layoutbox .vstack ([gslb , lb ], padding = pad * gslb .width )
651- else :
652- layoutbox .vstack ([lb , gslb ], padding = pad * gslb .width )
653-
654- maxcol = - 100000
655- mincol = 1000000
656- maxax = None
657- minax = None
658-
659687 for ax in parents :
660- subspec = ax .get_subplotspec ()
661- nrows , ncols = subspec .get_gridspec ().get_geometry ()
662- for num in [subspec .num1 , subspec .num2 ]:
663- rownum1 , colnum1 = divmod (subspec .num1 , ncols )
664- if colnum1 > maxcol :
665- maxcol = colnum1
666- maxax = ax
667- if rownum1 < mincol :
668- mincol = colnum1
669- minax = ax
670- maxposlb = maxax ._poslayoutbox
671- minposlb = minax ._poslayoutbox
688+ if location == 'bottom' :
689+ order = [ax ._layoutbox , lb ]
690+ else :
691+ order = [lb , ax ._layoutbox ]
692+ layoutbox .vstack (order , padding = pad * gslb .width ,
693+ strength = 'strong' )
694+
695+ # Vertical Layout: need to check all the axes in this gridspec
696+ for ch in gslb .children :
697+ subspec = ch .artist
698+ nrows , ncols , row_start , row_stop , col_start , col_stop = \
699+ subspec .get_rows_columns ()
700+ if location == 'bottom' :
701+ if row_stop <= minrow :
702+ order = [subspec ._layoutbox , lb ]
703+ if row_start > maxrow :
704+ order = [lb , subspec ._layoutbox ]
705+ elif location == 'top' :
706+ if row_stop < minrow :
707+ order = [subspec ._layoutbox , lb ]
708+ if row_start >= maxrow :
709+ order = [lb , subspec ._layoutbox ]
710+ layoutbox .vstack (order , padding = pad * gslb .width ,
711+ strength = 'strong' )
712+
713+ # Do horizontal layout...
714+ maxposlb = maxax_col ._poslayoutbox
715+ minposlb = minax_col ._poslayoutbox
672716 lbpos .constrain_width ((maxposlb .right - minposlb .left ) *
673717 shrink )
674718 lbpos .constrain_left (
0 commit comments