@@ -578,18 +578,25 @@ class HandlerTuple(HandlerBase):
578
578
The number of sections to divide the legend area into. If None,
579
579
use the length of the input tuple. Default is 1.
580
580
581
-
582
581
pad : float, optional
583
582
If None, fall back to `legend.borderpad` as the default.
584
583
In units of fraction of font size. Default is None.
585
584
586
-
585
+ width_ratios : tuple, optional
586
+ The relative width of sections. Must be of length ndivide.
587
+ If None, all sections will have the same width. Default is None.
587
588
588
589
"""
589
- def __init__ (self , ndivide = 1 , pad = None , ** kwargs ):
590
+ def __init__ (self , ndivide = 1 , pad = None , width_ratios = None , ** kwargs ):
590
591
591
592
self ._ndivide = ndivide
592
593
self ._pad = pad
594
+
595
+ if (width_ratios is not None ) and (len (width_ratios ) == ndivide ):
596
+ self ._width_ratios = width_ratios
597
+ else :
598
+ self ._width_ratios = None
599
+
593
600
HandlerBase .__init__ (self , ** kwargs )
594
601
595
602
def create_artists (self , legend , orig_handle ,
@@ -608,10 +615,16 @@ def create_artists(self, legend, orig_handle,
608
615
else :
609
616
pad = self ._pad * fontsize
610
617
611
- if ndivide > 1 :
612
- width = (width - pad * (ndivide - 1 )) / ndivide
618
+ if self ._width_ratios is not None :
619
+ sumratios = sum (self ._width_ratios )
620
+ widths = [(width - pad * (ndivide - 1 )) * ratio / sumratios
621
+ for ratio in self ._width_ratios ]
622
+ else :
623
+ widths = [(width - pad * (ndivide - 1 )) / ndivide
624
+ for _ in range (ndivide )]
625
+ widths_cycle = cycle (widths )
613
626
614
- xds = [xdescent - (width + pad ) * i for i in range (ndivide )]
627
+ xds = [xdescent - (widths [ - i - 1 ] + pad ) * i for i in range (ndivide )]
615
628
xds_cycle = cycle (xds )
616
629
617
630
a_list = []
@@ -620,7 +633,8 @@ def create_artists(self, legend, orig_handle,
620
633
_a_list = handler .create_artists (legend , handle1 ,
621
634
six .next (xds_cycle ),
622
635
ydescent ,
623
- width , height ,
636
+ six .next (widths_cycle ),
637
+ height ,
624
638
fontsize ,
625
639
trans )
626
640
a_list .extend (_a_list )
0 commit comments