@@ -712,27 +712,19 @@ def _init_legend_box(self, handles, labels, markerfirst=True):
712712
713713 fontsize = self ._fontsize
714714
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.
722720
723721 text_list = [] # the list of text instances
724722 handle_list = [] # the list of handle instances
725723 handles_and_labels = []
726724
727- label_prop = dict (verticalalignment = 'baseline' ,
728- horizontalalignment = 'left' ,
729- fontproperties = self .prop ,
730- )
731-
732725 # The approximate height and descent of text. These values are
733726 # 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.
736728 height = fontsize * self .handleheight - descent
737729 # each handle needs to be drawn inside a box of (x, y, w, h) =
738730 # (0, -descent, width, height). And their coordinates should
@@ -744,7 +736,7 @@ def _init_legend_box(self, handles, labels, markerfirst=True):
744736 # manually set their transform to the self.get_transform().
745737 legend_handler_map = self .get_legend_handler_map ()
746738
747- for orig_handle , lab in zip (handles , labels ):
739+ for orig_handle , label in zip (handles , labels ):
748740 handler = self .get_legend_handler (legend_handler_map , orig_handle )
749741 if handler is None :
750742 _api .warn_external (
@@ -753,12 +745,14 @@ def _init_legend_box(self, handles, labels, markerfirst=True):
753745 "https://matplotlib.org/users/legend_guide.html"
754746 "#creating-artists-specifically-for-adding-to-the-legend-"
755747 "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.
758749 handle_list .append (None )
759750 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 ))
762756 handlebox = DrawingArea (width = self .handlelength * fontsize ,
763757 height = height ,
764758 xdescent = 0. , ydescent = descent )
@@ -770,34 +764,25 @@ def _init_legend_box(self, handles, labels, markerfirst=True):
770764 fontsize , handlebox ))
771765 handles_and_labels .append ((handlebox , textbox ))
772766
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-
787767 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 ,
791776 sep = self .handletextpad * fontsize ,
792777 children = [h , t ] if markerfirst else [t , h ],
793778 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
796781 alignment = "baseline" if markerfirst else "right"
797782 columnbox .append (VPacker (pad = 0 ,
798783 sep = self .labelspacing * fontsize ,
799784 align = alignment ,
800- children = itemBoxes ))
785+ children = itemboxes ))
801786
802787 mode = "expand" if self ._mode == "expand" else "fixed"
803788 sep = self .columnspacing * fontsize
0 commit comments