@@ -712,27 +712,19 @@ def _init_legend_box(self, handles, labels, markerfirst=True):
712
712
713
713
fontsize = self ._fontsize
714
714
715
- # legend_box is a HPacker, horizontally packed with
716
- # columns. Each column is a VPacker, vertically packed with
717
- # legend items. Each legend item is HPacker packed with
718
- # legend handleBox and labelBox. handleBox is an instance of
719
- # offsetbox.DrawingArea which contains legend handle. labelBox
720
- # is an instance of offsetbox.TextArea which contains legend
721
- # text.
715
+ # legend_box is a HPacker, horizontally packed with columns.
716
+ # Each column is a VPacker, vertically packed with legend items.
717
+ # Each legend item is a HPacker packed with:
718
+ # - handlebox: a DrawingArea which contains the legend handle.
719
+ # - labelbox: a TextArea which contains the legend text.
722
720
723
721
text_list = [] # the list of text instances
724
722
handle_list = [] # the list of handle instances
725
723
handles_and_labels = []
726
724
727
- label_prop = dict (verticalalignment = 'baseline' ,
728
- horizontalalignment = 'left' ,
729
- fontproperties = self .prop ,
730
- )
731
-
732
725
# The approximate height and descent of text. These values are
733
726
# only used for plotting the legend handle.
734
- descent = 0.35 * fontsize * (self .handleheight - 0.7 )
735
- # 0.35 and 0.7 are just heuristic numbers and may need to be improved.
727
+ descent = 0.35 * fontsize * (self .handleheight - 0.7 ) # heuristic.
736
728
height = fontsize * self .handleheight - descent
737
729
# each handle needs to be drawn inside a box of (x, y, w, h) =
738
730
# (0, -descent, width, height). And their coordinates should
@@ -744,7 +736,7 @@ def _init_legend_box(self, handles, labels, markerfirst=True):
744
736
# manually set their transform to the self.get_transform().
745
737
legend_handler_map = self .get_legend_handler_map ()
746
738
747
- for orig_handle , lab in zip (handles , labels ):
739
+ for orig_handle , label in zip (handles , labels ):
748
740
handler = self .get_legend_handler (legend_handler_map , orig_handle )
749
741
if handler is None :
750
742
_api .warn_external (
@@ -753,12 +745,14 @@ def _init_legend_box(self, handles, labels, markerfirst=True):
753
745
"https://matplotlib.org/users/legend_guide.html"
754
746
"#creating-artists-specifically-for-adding-to-the-legend-"
755
747
"aka-proxy-artists" .format (orig_handle ))
756
- # We don't have a handle for this artist, so we just defer
757
- # to None.
748
+ # No handle for this artist, so we just defer to None.
758
749
handle_list .append (None )
759
750
else :
760
- textbox = TextArea (lab , textprops = label_prop ,
761
- multilinebaseline = True )
751
+ textbox = TextArea (label , multilinebaseline = True ,
752
+ textprops = dict (
753
+ verticalalignment = 'baseline' ,
754
+ horizontalalignment = 'left' ,
755
+ fontproperties = self .prop ))
762
756
handlebox = DrawingArea (width = self .handlelength * fontsize ,
763
757
height = height ,
764
758
xdescent = 0. , ydescent = descent )
@@ -770,34 +764,25 @@ def _init_legend_box(self, handles, labels, markerfirst=True):
770
764
fontsize , handlebox ))
771
765
handles_and_labels .append ((handlebox , textbox ))
772
766
773
- if handles_and_labels :
774
- # We calculate number of rows in each column. The first
775
- # (num_largecol) columns will have (nrows+1) rows, and remaining
776
- # (num_smallcol) columns will have (nrows) rows.
777
- ncol = min (self ._ncol , len (handles_and_labels ))
778
- nrows , num_largecol = divmod (len (handles_and_labels ), ncol )
779
- num_smallcol = ncol - num_largecol
780
- # starting index of each column and number of rows in it.
781
- rows_per_col = [nrows + 1 ] * num_largecol + [nrows ] * num_smallcol
782
- start_idxs = np .concatenate ([[0 ], np .cumsum (rows_per_col )[:- 1 ]])
783
- cols = zip (start_idxs , rows_per_col )
784
- else :
785
- cols = []
786
-
787
767
columnbox = []
788
- for i0 , di in cols :
789
- # pack handleBox and labelBox into itemBox
790
- itemBoxes = [HPacker (pad = 0 ,
768
+ # array_split splits n handles_and_labels into ncol columns, with the
769
+ # first n%ncol columns having an extra entry. filter(len, ...) handles
770
+ # the case where n < ncol: the last ncol-n columns are empty and get
771
+ # filtered out.
772
+ for handles_and_labels_column \
773
+ in filter (len , np .array_split (handles_and_labels , self ._ncol )):
774
+ # pack handlebox and labelbox into itembox
775
+ itemboxes = [HPacker (pad = 0 ,
791
776
sep = self .handletextpad * fontsize ,
792
777
children = [h , t ] if markerfirst else [t , h ],
793
778
align = "baseline" )
794
- for h , t in handles_and_labels [ i0 : i0 + di ] ]
795
- # pack columnBox
779
+ for h , t in handles_and_labels_column ]
780
+ # pack columnbox
796
781
alignment = "baseline" if markerfirst else "right"
797
782
columnbox .append (VPacker (pad = 0 ,
798
783
sep = self .labelspacing * fontsize ,
799
784
align = alignment ,
800
- children = itemBoxes ))
785
+ children = itemboxes ))
801
786
802
787
mode = "expand" if self ._mode == "expand" else "fixed"
803
788
sep = self .columnspacing * fontsize
0 commit comments