@@ -555,6 +555,36 @@ def layoutcolorbarsingle(ax, cax, shrink, aspect, location, pad=0.05):
555
555
return lb , lbpos
556
556
557
557
558
+ def _getmaxminrowcolumn (axs ):
559
+ # helper to get the min/max rows and columns of a list of axes.
560
+ maxrow = - 100000
561
+ minrow = 1000000
562
+ maxax = None
563
+ minax = None
564
+ maxcol = - 100000
565
+ mincol = 1000000
566
+ maxax_col = None
567
+ minax_col = None
568
+
569
+ for ax in axs :
570
+ subspec = ax .get_subplotspec ()
571
+ nrows , ncols , row_start , row_stop , col_start , col_stop = \
572
+ subspec .get_rows_columns ()
573
+ if row_stop > maxrow :
574
+ maxrow = row_stop
575
+ maxax = ax
576
+ if row_start < minrow :
577
+ minrow = row_start
578
+ minax = ax
579
+ if col_stop > maxcol :
580
+ maxcol = col_stop
581
+ maxax_col = ax
582
+ if col_start < mincol :
583
+ mincol = col_start
584
+ minax_col = ax
585
+ return (minrow , maxrow , minax , maxax , mincol , maxcol , minax_col , maxax_col )
586
+
587
+
558
588
def layoutcolorbargridspec (parents , cax , shrink , aspect , location , pad = 0.05 ):
559
589
"""
560
590
Do the layout for a colorbar, to not oeverly pollute colorbar.py
@@ -569,6 +599,10 @@ def layoutcolorbargridspec(parents, cax, shrink, aspect, location, pad=0.05):
569
599
lb = layoutbox .LayoutBox (parent = gslb .parent ,
570
600
name = gslb .parent .name + '.cbar' ,
571
601
artist = cax )
602
+ # figure out the row and column extent of the parents.
603
+ (minrow , maxrow , minax_row , maxax_row ,
604
+ mincol , maxcol , minax_col , maxax_col ) = _getmaxminrowcolumn (parents )
605
+
572
606
if location in ('left' , 'right' ):
573
607
lbpos = layoutbox .LayoutBox (
574
608
parent = lb ,
@@ -577,39 +611,43 @@ def layoutcolorbargridspec(parents, cax, shrink, aspect, location, pad=0.05):
577
611
pos = True ,
578
612
subplot = False ,
579
613
artist = cax )
580
-
581
- if location == 'right' :
582
- # arrange to right of the gridpec sibbling
583
- layoutbox . hstack ([ gslb , lb ], padding = pad * gslb . width ,
584
- strength = 'strong' )
585
- else :
586
- layoutbox . hstack ([ lb , gslb ], padding = pad * gslb . width )
614
+ for ax in parents :
615
+ if location == 'right' :
616
+ order = [ ax . _layoutbox , lb ]
617
+ else :
618
+ order = [ lb , ax . _layoutbox ]
619
+ layoutbox . hstack ( order , padding = pad * gslb . width ,
620
+ strength = 'strong' )
587
621
# constrain the height and center...
588
622
# This isn't quite right. We'd like the colorbar
589
623
# pos to line up w/ the axes poss, not the size of the
590
624
# gs.
591
- maxrow = - 100000
592
- minrow = 1000000
593
- maxax = None
594
- minax = None
595
625
596
- for ax in parents :
597
- subspec = ax .get_subplotspec ()
598
- nrows , ncols = subspec .get_gridspec ().get_geometry ()
599
- for num in [subspec .num1 , subspec .num2 ]:
600
- rownum1 , colnum1 = divmod (subspec .num1 , ncols )
601
- if rownum1 > maxrow :
602
- maxrow = rownum1
603
- maxax = ax
604
- if rownum1 < minrow :
605
- minrow = rownum1
606
- minax = ax
607
- # invert the order so these are bottom to top:
608
- maxposlb = minax ._poslayoutbox
609
- minposlb = maxax ._poslayoutbox
626
+ # Horizontal Layout: need to check all the axes in this gridspec
627
+ for ch in gslb .children :
628
+ subspec = ch .artist
629
+ nrows , ncols , row_start , row_stop , col_start , col_stop = \
630
+ subspec .get_rows_columns ()
631
+ if location == 'right' :
632
+ if col_stop <= maxcol :
633
+ order = [subspec ._layoutbox , lb ]
634
+ # arrange to right of the parents
635
+ if col_start > maxcol :
636
+ order = [lb , subspec ._layoutbox ]
637
+ elif location == 'left' :
638
+ if col_start >= mincol :
639
+ order = [lb , subspec ._layoutbox ]
640
+ if col_stop < mincol :
641
+ order = [subspec ._layoutbox , lb ]
642
+ layoutbox .hstack (order , padding = pad * gslb .width ,
643
+ strength = 'strong' )
644
+
645
+ # Vertical layout:
646
+ maxposlb = minax_row ._poslayoutbox
647
+ minposlb = maxax_row ._poslayoutbox
610
648
# now we want the height of the colorbar pos to be
611
- # set by the top and bottom of these poss
612
- # bottom top
649
+ # set by the top and bottom of the min/max axes...
650
+ # bottom top
613
651
# b t
614
652
# h = (top-bottom)*shrink
615
653
# b = bottom + (top-bottom - h) / 2.
@@ -633,29 +671,35 @@ def layoutcolorbargridspec(parents, cax, shrink, aspect, location, pad=0.05):
633
671
subplot = False ,
634
672
artist = cax )
635
673
636
- if location == 'bottom' :
637
- layoutbox .vstack ([gslb , lb ], padding = pad * gslb .width )
638
- else :
639
- layoutbox .vstack ([lb , gslb ], padding = pad * gslb .width )
640
-
641
- maxcol = - 100000
642
- mincol = 1000000
643
- maxax = None
644
- minax = None
645
-
646
674
for ax in parents :
647
- subspec = ax .get_subplotspec ()
648
- nrows , ncols = subspec .get_gridspec ().get_geometry ()
649
- for num in [subspec .num1 , subspec .num2 ]:
650
- rownum1 , colnum1 = divmod (subspec .num1 , ncols )
651
- if colnum1 > maxcol :
652
- maxcol = colnum1
653
- maxax = ax
654
- if rownum1 < mincol :
655
- mincol = colnum1
656
- minax = ax
657
- maxposlb = maxax ._poslayoutbox
658
- minposlb = minax ._poslayoutbox
675
+ if location == 'bottom' :
676
+ order = [ax ._layoutbox , lb ]
677
+ else :
678
+ order = [lb , ax ._layoutbox ]
679
+ layoutbox .vstack (order , padding = pad * gslb .width ,
680
+ strength = 'strong' )
681
+
682
+ # Vertical Layout: need to check all the axes in this gridspec
683
+ for ch in gslb .children :
684
+ subspec = ch .artist
685
+ nrows , ncols , row_start , row_stop , col_start , col_stop = \
686
+ subspec .get_rows_columns ()
687
+ if location == 'bottom' :
688
+ if row_stop <= minrow :
689
+ order = [subspec ._layoutbox , lb ]
690
+ if row_start > maxrow :
691
+ order = [lb , subspec ._layoutbox ]
692
+ elif location == 'top' :
693
+ if row_stop < minrow :
694
+ order = [subspec ._layoutbox , lb ]
695
+ if row_start >= maxrow :
696
+ order = [lb , subspec ._layoutbox ]
697
+ layoutbox .vstack (order , padding = pad * gslb .width ,
698
+ strength = 'strong' )
699
+
700
+ # Do horizontal layout...
701
+ maxposlb = maxax_col ._poslayoutbox
702
+ minposlb = minax_col ._poslayoutbox
659
703
lbpos .constrain_width ((maxposlb .right - minposlb .left ) *
660
704
shrink )
661
705
lbpos .constrain_left (
0 commit comments