@@ -110,7 +110,8 @@ def __init__(self, parent, handles, labels,
110110 mode = None , # mode for horizontal distribution of columns. None, "expand"
111111
112112 fancybox = None , # True use a fancy box, false use a rounded box, none use rc
113- shadow = None ,
113+ shadow = None ,
114+ title = None , # set a title for the legend
114115 ):
115116 """
116117 - *parent* : the artist that contains the legend
@@ -135,6 +136,7 @@ def __init__(self, parent, handles, labels,
135136 handletextpad the pad between the legend handle and text
136137 borderaxespad the pad between the axes and legend border
137138 columnspacing the spacing between columns
139+ title the legend title
138140 ================ ==================================================================
139141
140142The dimensions of pad and spacing are given as a fraction of the
@@ -276,6 +278,8 @@ def __init__(self, parent, handles, labels,
276278 # init with null renderer
277279 self ._init_legend_box (handles , labels )
278280
281+ self .set_title (title )
282+
279283 self ._last_fontsize_points = self ._fontsize
280284
281285
@@ -316,6 +320,7 @@ def draw(self, renderer):
316320
317321 renderer .open_group ('legend' )
318322
323+
319324 # find_offset function will be provided to _legend_box and
320325 # _legend_box will draw itself at the location of the return
321326 # value of the find_offset.
@@ -562,10 +567,18 @@ def _init_legend_box(self, handles, labels):
562567
563568 sep = self .columnspacing * fontsize
564569
565- self ._legend_box = HPacker (pad = self .borderpad * fontsize ,
566- sep = sep , align = "baseline" ,
567- mode = mode ,
568- children = columnbox )
570+ self ._legend_handle_box = HPacker (pad = 0 ,
571+ sep = sep , align = "baseline" ,
572+ mode = mode ,
573+ children = columnbox )
574+
575+ self ._legend_title_box = TextArea ("" )
576+
577+ self ._legend_box = VPacker (pad = self .borderpad * fontsize ,
578+ sep = self .labelspacing * fontsize ,
579+ align = "center" ,
580+ children = [self ._legend_title_box ,
581+ self ._legend_handle_box ])
569582
570583 self ._legend_box .set_figure (self .figure )
571584
@@ -640,6 +653,19 @@ def get_texts(self):
640653 'return a list of text.Text instance in the legend'
641654 return silent_list ('Text' , self .texts )
642655
656+ def set_title (self , title ):
657+ 'set the legend title'
658+ self ._legend_title_box ._text .set_text (title )
659+
660+ if title :
661+ self ._legend_title_box .set_visible (True )
662+ else :
663+ self ._legend_title_box .set_visible (False )
664+
665+ def get_title (self ):
666+ 'return Text instance for the legend title'
667+ return self ._legend_title_box ._text
668+
643669 def get_window_extent (self ):
644670 'return a extent of the the legend'
645671 return self .legendPatch .get_window_extent ()
0 commit comments