Hello,

I sometimes want to add titles to legends in matplotlib. I couldn't find
an existing way to do this (but if there is one, then please tell me!).
So I have hacked together a patch to add a title keyword to the legend
class, which adds the title to the top of the legend. I'm not sure it
conforms very well to matplotlib style, but I have attached it to this
message in case anyone else finds it useful.

Cheers,
John
Index: lib/matplotlib/legend.py
===================================================================
--- lib/matplotlib/legend.py	(revision 4937)
+++ lib/matplotlib/legend.py	(working copy)
@@ -92,7 +92,8 @@
                  handletextsep = None, # the space between the legend line and legend text
                  axespad = None,       # the border between the axes and legend edge
 
-                 shadow = None
+                 shadow = None,
+                 title = None
                  ):
         """
   parent                # the artist that contains the legend
@@ -104,6 +105,7 @@
   pad = 0.2             # the fractional whitespace inside the legend border
   markerscale = 0.6     # the relative size of legend markers vs. original
   shadow                # if True, draw a shadow behind legend
+  title                 # set a title for the legend
 
 The following dimensions are in axes coords
   labelsep = 0.005     # the vertical space between the legend entries
@@ -178,7 +180,22 @@
         # based on it's bbox
         left, top = 0.5, 0.5
         textleft = left+ self.handlelen+self.handletextsep
-        self.texts = self._get_texts(labels, textleft, top)
+        
+        ntop = top
+        self.title = None
+        if title != None:
+            ntop = ntop - self._approx_text_height()
+            text = Text(
+                x=textleft, y=top,
+                text=title,
+                fontproperties=self.prop,
+                verticalalignment='top',
+                horizontalalignment='center'
+                )
+            self._set_artist_props(text)
+            self.title = text
+
+        self.texts = self._get_texts(labels, textleft, ntop)
         self.legendHandles = self._get_handles(handles, self.texts)
 
         self._drawFrame = True
@@ -208,6 +225,9 @@
                 h.draw(renderer)
                 if 0: bbox_artist(h, renderer)
 
+        if self.title != None:
+            self.title.draw(renderer)
+
         for t in self.texts:
             if 0: bbox_artist(t, renderer)
             t.draw(renderer)
@@ -223,6 +243,11 @@
 
         bboxesAll = bboxesText
         bboxesAll.extend(bboxesHandles)
+
+        if self.title != None:
+            bboxesTitle = self.title.get_window_extent(renderer)
+            bboxesAll.extend([bboxesTitle])
+
         bbox = Bbox.union(bboxesAll)
 
         self.save = bbox
@@ -491,6 +516,11 @@
         h += 2*self.labelsep
         hpos.append( (b,h) )
 
+        if self.title != None:
+            x,y = self.title.get_position()
+            l,b,w,h = get_tbounds(self.texts[0])
+            self.title.set_position( (x, b+3*h) )
+
         for handle, tup in zip(self.legendHandles, hpos):
             y,h = tup
             if isinstance(handle, Line2D):
-------------------------------------------------------------------------
This SF.net email is sponsored by: Microsoft
Defy all challenges. Microsoft(R) Visual Studio 2008.
http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/
_______________________________________________
Matplotlib-users mailing list
Matplotlib-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/matplotlib-users

Reply via email to