Revision: 4871
          http://matplotlib.svn.sourceforge.net/matplotlib/?rev=4871&view=rev
Author:   mdboom
Date:     2008-01-15 12:52:27 -0800 (Tue, 15 Jan 2008)

Log Message:
-----------
Make numpoints=1 work for legend handles.  (Patch submitted by Paul Novak).

Modified Paths:
--------------
    trunk/matplotlib/lib/matplotlib/legend.py

Modified: trunk/matplotlib/lib/matplotlib/legend.py
===================================================================
--- trunk/matplotlib/lib/matplotlib/legend.py   2008-01-15 20:00:50 UTC (rev 
4870)
+++ trunk/matplotlib/lib/matplotlib/legend.py   2008-01-15 20:52:27 UTC (rev 
4871)
@@ -175,10 +175,6 @@
         # make a trial box in the middle of the axes.  relocate it
         # based on it's bbox
         left, top = 0.5, 0.5
-        if self.numpoints == 1:
-            self._xdata = npy.array([left + self.handlelen*0.5])
-        else:
-            self._xdata = npy.linspace(left, left + self.handlelen, 
self.numpoints)
         textleft = left+ self.handlelen+self.handletextsep
         self.texts = self._get_texts(labels, textleft, top)
         self.legendHandles = self._get_handles(handles, self.texts)
@@ -236,15 +232,23 @@
 
     def _get_handles(self, handles, texts):
         HEIGHT = self._approx_text_height()
+        left = 0.5
 
         ret = []   # the returned legend lines
 
         for handle, label in zip(handles, texts):
+            if self.numpoints > 1:
+                xdata = npy.linspace(left, left + self.handlelen, 
self.numpoints)
+            elif self.numpoints == 1:
+                xdata = npy.linspace(left, left + self.handlelen, 2)
+
             x, y = label.get_position()
             x -= self.handlelen + self.handletextsep
             if isinstance(handle, Line2D):
-                ydata = (y-HEIGHT/2)*npy.ones(self._xdata.shape, float)
-                legline = Line2D(self._xdata, ydata)
+                if self.numpoints == 1 and handle._marker != 'None':
+                    xdata = npy.array([left + self.handlelen*0.5])
+                ydata = (y-HEIGHT/2)*npy.ones(xdata.shape, float)
+                legline = Line2D(xdata, ydata)
                 legline.update_from(handle)
                 self._set_artist_props(legline) # after update
                 legline.set_clip_box(None)
@@ -253,8 +257,7 @@
 
                 ret.append(legline)
             elif isinstance(handle, Patch):
-
-                p = Rectangle(xy=(min(self._xdata), y-3/4*HEIGHT),
+                p = Rectangle(xy=(min(xdata), y-3/4*HEIGHT),
                               width = self.handlelen, height=HEIGHT/2,
                               )
                 p.update_from(handle)
@@ -263,8 +266,8 @@
                 p.set_clip_path(None)
                 ret.append(p)
             elif isinstance(handle, LineCollection):
-                ydata = (y-HEIGHT/2)*npy.ones(self._xdata.shape, float)
-                legline = Line2D(self._xdata, ydata)
+                ydata = (y-HEIGHT/2)*npy.ones(xdata.shape, float)
+                legline = Line2D(xdata, ydata)
                 self._set_artist_props(legline)
                 legline.set_clip_box(None)
                 legline.set_clip_path(None)
@@ -277,7 +280,9 @@
                 ret.append(legline)
 
             elif isinstance(handle, RegularPolyCollection):
-                p = Rectangle(xy=(min(self._xdata), y-3/4*HEIGHT),
+                if self.numpoints == 1:
+                    xdata = npy.array([left])
+                p = Rectangle(xy=(min(xdata), y-3/4*HEIGHT),
                               width = self.handlelen, height=HEIGHT/2,
                               )
                 p.set_facecolor(handle._facecolors[0])
@@ -487,7 +492,7 @@
         for handle, tup in zip(self.legendHandles, hpos):
             y,h = tup
             if isinstance(handle, Line2D):
-                ydata = y*npy.ones(self._xdata.shape, float)
+                ydata = y*npy.ones(handle.get_xdata().shape, float)
                 handle.set_ydata(ydata+h/2)
             elif isinstance(handle, Rectangle):
                 handle.set_y(y+1/4*h)


This was sent by the SourceForge.net collaborative development platform, the 
world's largest Open Source development site.

-------------------------------------------------------------------------
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-checkins mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/matplotlib-checkins

Reply via email to