33
33
34
34
from matplotlib import rcParams
35
35
from matplotlib .artist import Artist , allow_rasterization
36
- from matplotlib .cbook import (is_string_like , iterable , silent_list , safezip ,
37
- is_hashable )
36
+ from matplotlib .cbook import is_string_like , iterable , silent_list , is_hashable
38
37
from matplotlib .font_manager import FontProperties
39
38
from matplotlib .lines import Line2D
40
39
from matplotlib .patches import Patch , Rectangle , Shadow , FancyBboxPatch
@@ -653,29 +652,23 @@ def _init_legend_box(self, handles, labels, markerfirst=True):
653
652
handle_list .append (handler .legend_artist (self , orig_handle ,
654
653
fontsize , handlebox ))
655
654
656
- if len (handleboxes ) > 0 :
657
-
655
+ if handleboxes :
658
656
# We calculate number of rows in each column. The first
659
657
# (num_largecol) columns will have (nrows+1) rows, and remaining
660
658
# (num_smallcol) columns will have (nrows) rows.
661
659
ncol = min (self ._ncol , len (handleboxes ))
662
660
nrows , num_largecol = divmod (len (handleboxes ), ncol )
663
661
num_smallcol = ncol - num_largecol
664
-
665
662
# starting index of each column and number of rows in it.
666
- largecol = safezip (list (xrange (0 ,
667
- num_largecol * (nrows + 1 ),
668
- (nrows + 1 ))),
669
- [nrows + 1 ] * num_largecol )
670
- smallcol = safezip (list (xrange (num_largecol * (nrows + 1 ),
671
- len (handleboxes ), nrows )),
672
- [nrows ] * num_smallcol )
663
+ rows_per_col = [nrows + 1 ] * num_largecol + [nrows ] * num_smallcol
664
+ start_idxs = np .concatenate ([[0 ], np .cumsum (rows_per_col )[:- 1 ]])
665
+ cols = zip (start_idxs , rows_per_col )
673
666
else :
674
- largecol , smallcol = [], []
667
+ cols = []
675
668
676
- handle_label = safezip ( handleboxes , labelboxes )
669
+ handle_label = list ( zip ( handleboxes , labelboxes ) )
677
670
columnbox = []
678
- for i0 , di in largecol + smallcol :
671
+ for i0 , di in cols :
679
672
# pack handleBox and labelBox into itemBox
680
673
itemBoxes = [HPacker (pad = 0 ,
681
674
sep = self .handletextpad * fontsize ,
@@ -689,20 +682,13 @@ def _init_legend_box(self, handles, labels, markerfirst=True):
689
682
itemBoxes [- 1 ].get_children ()[0 ].set_minimumdescent (False )
690
683
691
684
# pack columnBox
692
- if markerfirst :
693
- alignment = "baseline"
694
- else :
695
- alignment = "right"
685
+ alignment = "baseline" if markerfirst else "right"
696
686
columnbox .append (VPacker (pad = 0 ,
697
687
sep = self .labelspacing * fontsize ,
698
688
align = alignment ,
699
689
children = itemBoxes ))
700
690
701
- if self ._mode == "expand" :
702
- mode = "expand"
703
- else :
704
- mode = "fixed"
705
-
691
+ mode = "expand" if self ._mode == "expand" else "fixed"
706
692
sep = self .columnspacing * fontsize
707
693
self ._legend_handle_box = HPacker (pad = 0 ,
708
694
sep = sep , align = "baseline" ,
0 commit comments