@@ -29,6 +29,7 @@ def legend_artist(self, legend, orig_handle, fontsize, handlebox):
2929
3030from matplotlib .externals import six
3131from matplotlib .externals .six .moves import zip
32+ from itertools import cycle
3233
3334import numpy as np
3435
@@ -150,13 +151,14 @@ def get_xdata(self, legend, xdescent, ydescent, width, height, fontsize):
150151 if numpoints > 1 :
151152 # we put some pad here to compensate the size of the
152153 # marker
153- xdata = np .linspace (- xdescent + self ._marker_pad * fontsize ,
154- width - self ._marker_pad * fontsize ,
154+ pad = self ._marker_pad * fontsize
155+ xdata = np .linspace (- xdescent + pad ,
156+ - xdescent + width - pad ,
155157 numpoints )
156158 xdata_marker = xdata
157159 elif numpoints == 1 :
158- xdata = np .linspace (- xdescent , width , 2 )
159- xdata_marker = [0.5 * width - 0.5 * xdescent ]
160+ xdata = np .linspace (- xdescent , - xdescent + width , 2 )
161+ xdata_marker = [- xdescent + 0.5 * width ]
160162
161163 return xdata , xdata_marker
162164
@@ -499,6 +501,7 @@ def create_artists(self, legend, orig_handle,
499501
500502 return artists
501503
504+
502505class HandlerStem (HandlerNpointsYoffsets ):
503506 """
504507 Handler for Errorbars
@@ -565,21 +568,60 @@ def create_artists(self, legend, orig_handle,
565568
566569class HandlerTuple (HandlerBase ):
567570 """
568- Handler for Tuple
571+ Handler for Tuple.
572+
573+ Additional kwargs are passed through to `HandlerBase`.
574+
575+ Parameters
576+ ----------
577+
578+ ndivide : int, optional
579+ The number of sections to divide the legend area into. If None,
580+ use the length of the input tuple. Default is 1.
581+
582+
583+ pad : float, optional
584+ If None, fall back to `legend.borderpad` as the default.
585+ In units of fraction of font size. Default is None.
586+
587+
588+
569589 """
570- def __init__ (self , ** kwargs ):
590+ def __init__ (self , ndivide = 1 , pad = None , ** kwargs ):
591+
592+ self ._ndivide = ndivide
593+ self ._pad = pad
571594 HandlerBase .__init__ (self , ** kwargs )
572595
573596 def create_artists (self , legend , orig_handle ,
574597 xdescent , ydescent , width , height , fontsize ,
575598 trans ):
576599
577600 handler_map = legend .get_legend_handler_map ()
601+
602+ if self ._ndivide is None :
603+ ndivide = len (orig_handle )
604+ else :
605+ ndivide = self ._ndivide
606+
607+ if self ._pad is None :
608+ pad = legend .borderpad * fontsize
609+ else :
610+ pad = self ._pad * fontsize
611+
612+ if ndivide > 1 :
613+ width = (width - pad * (ndivide - 1 )) / ndivide
614+
615+ xds = [xdescent - (width + pad ) * i for i in range (ndivide )]
616+ xds_cycle = cycle (xds )
617+
578618 a_list = []
579619 for handle1 in orig_handle :
580620 handler = legend .get_legend_handler (handler_map , handle1 )
581621 _a_list = handler .create_artists (legend , handle1 ,
582- xdescent , ydescent , width , height ,
622+ six .next (xds_cycle ),
623+ ydescent ,
624+ width , height ,
583625 fontsize ,
584626 trans )
585627 a_list .extend (_a_list )
0 commit comments