Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit be76b74

Browse files
committed
Make numpoints=1 work for legend handles. (Patch submitted by Paul Novak).
svn path=/trunk/matplotlib/; revision=4871
1 parent 9285cec commit be76b74

1 file changed

Lines changed: 17 additions & 12 deletions

File tree

lib/matplotlib/legend.py

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -175,10 +175,6 @@ def __init__(self, parent, handles, labels,
175175
# make a trial box in the middle of the axes. relocate it
176176
# based on it's bbox
177177
left, top = 0.5, 0.5
178-
if self.numpoints == 1:
179-
self._xdata = npy.array([left + self.handlelen*0.5])
180-
else:
181-
self._xdata = npy.linspace(left, left + self.handlelen, self.numpoints)
182178
textleft = left+ self.handlelen+self.handletextsep
183179
self.texts = self._get_texts(labels, textleft, top)
184180
self.legendHandles = self._get_handles(handles, self.texts)
@@ -236,15 +232,23 @@ def _get_handle_text_bbox(self, renderer):
236232

237233
def _get_handles(self, handles, texts):
238234
HEIGHT = self._approx_text_height()
235+
left = 0.5
239236

240237
ret = [] # the returned legend lines
241238

242239
for handle, label in zip(handles, texts):
240+
if self.numpoints > 1:
241+
xdata = npy.linspace(left, left + self.handlelen, self.numpoints)
242+
elif self.numpoints == 1:
243+
xdata = npy.linspace(left, left + self.handlelen, 2)
244+
243245
x, y = label.get_position()
244246
x -= self.handlelen + self.handletextsep
245247
if isinstance(handle, Line2D):
246-
ydata = (y-HEIGHT/2)*npy.ones(self._xdata.shape, float)
247-
legline = Line2D(self._xdata, ydata)
248+
if self.numpoints == 1 and handle._marker != 'None':
249+
xdata = npy.array([left + self.handlelen*0.5])
250+
ydata = (y-HEIGHT/2)*npy.ones(xdata.shape, float)
251+
legline = Line2D(xdata, ydata)
248252
legline.update_from(handle)
249253
self._set_artist_props(legline) # after update
250254
legline.set_clip_box(None)
@@ -253,8 +257,7 @@ def _get_handles(self, handles, texts):
253257

254258
ret.append(legline)
255259
elif isinstance(handle, Patch):
256-
257-
p = Rectangle(xy=(min(self._xdata), y-3/4*HEIGHT),
260+
p = Rectangle(xy=(min(xdata), y-3/4*HEIGHT),
258261
width = self.handlelen, height=HEIGHT/2,
259262
)
260263
p.update_from(handle)
@@ -263,8 +266,8 @@ def _get_handles(self, handles, texts):
263266
p.set_clip_path(None)
264267
ret.append(p)
265268
elif isinstance(handle, LineCollection):
266-
ydata = (y-HEIGHT/2)*npy.ones(self._xdata.shape, float)
267-
legline = Line2D(self._xdata, ydata)
269+
ydata = (y-HEIGHT/2)*npy.ones(xdata.shape, float)
270+
legline = Line2D(xdata, ydata)
268271
self._set_artist_props(legline)
269272
legline.set_clip_box(None)
270273
legline.set_clip_path(None)
@@ -277,7 +280,9 @@ def _get_handles(self, handles, texts):
277280
ret.append(legline)
278281

279282
elif isinstance(handle, RegularPolyCollection):
280-
p = Rectangle(xy=(min(self._xdata), y-3/4*HEIGHT),
283+
if self.numpoints == 1:
284+
xdata = npy.array([left])
285+
p = Rectangle(xy=(min(xdata), y-3/4*HEIGHT),
281286
width = self.handlelen, height=HEIGHT/2,
282287
)
283288
p.set_facecolor(handle._facecolors[0])
@@ -487,7 +492,7 @@ def get_tbounds(text): #get text bounds in axes coords
487492
for handle, tup in zip(self.legendHandles, hpos):
488493
y,h = tup
489494
if isinstance(handle, Line2D):
490-
ydata = y*npy.ones(self._xdata.shape, float)
495+
ydata = y*npy.ones(handle.get_xdata().shape, float)
491496
handle.set_ydata(ydata+h/2)
492497
elif isinstance(handle, Rectangle):
493498
handle.set_y(y+1/4*h)

0 commit comments

Comments
 (0)