@@ -207,6 +207,11 @@ def __init__(self, parent, handles, labels,
207207 reps = int (self .numpoints / len (self ._scatteryoffsets )) + 1
208208 self ._scatteryoffsets = np .tile (self ._scatteryoffsets , reps )[:self .scatterpoints ]
209209
210+ # handles & labels (which can be iterators) need to be
211+ # explicitly converted to list.
212+ self ._handles_labels = list (handles ), list (labels )
213+
214+
210215 # _legend_box is an OffsetBox instance that contains all
211216 # legend items and will be initialized from _init_legend_box()
212217 # method.
@@ -273,9 +278,9 @@ def __init__(self, parent, handles, labels,
273278
274279 self ._drawFrame = True
275280
276- # populate the legend_box with legend items.
277- self ._init_legend_box (handles , labels )
278- self ._legend_box .set_figure (self .figure )
281+ # init with null renderer
282+ # self._init_legend_box(handles, labels, None )
283+ # self._legend_box.set_figure(self.figure)
279284
280285
281286 def _set_artist_props (self , a ):
@@ -294,7 +299,7 @@ def _findoffset_best(self, width, height, xdescent, ydescent):
294299 ox , oy = self ._find_best_position (width , height )
295300 return ox + xdescent , oy + ydescent
296301
297- def _findoffset_loc (self , width , height , xdescent , ydescent ):
302+ def _findoffset_loc (self , width , height , xdescent , ydescent , renderer ):
298303 "Heper function to locate the legend using the location code"
299304
300305 if iterable (self ._loc ) and len (self ._loc )== 2 :
@@ -304,14 +309,19 @@ def _findoffset_loc(self, width, height, xdescent, ydescent):
304309 x , y = bbox .x0 + bbox .width * fx , bbox .y0 + bbox .height * fy
305310 else :
306311 bbox = Bbox .from_bounds (0 , 0 , width , height )
307- x , y = self ._get_anchored_bbox (self ._loc , bbox , self .parent .bbox )
312+ x , y = self ._get_anchored_bbox (self ._loc , bbox , self .parent .bbox , renderer )
308313
309314 return x + xdescent , y + ydescent
310315
311316 def draw (self , renderer ):
312317 "Draw everything that belongs to the legend"
313318 if not self .get_visible (): return
314319
320+ # populate the legend_box with legend items.
321+ handles , labels = self ._handles_labels
322+ self ._init_legend_box (handles , labels , renderer )
323+ self ._legend_box .set_figure (self .figure )
324+
315325 renderer .open_group ('legend' )
316326
317327 # find_offset function will be provided to _legend_box and
@@ -320,12 +330,16 @@ def draw(self, renderer):
320330 if self ._loc == 0 :
321331 self ._legend_box .set_offset (self ._findoffset_best )
322332 else :
323- self ._legend_box .set_offset (self ._findoffset_loc )
333+ def _findoffset_loc (width , height , xdescent , ydescent ):
334+ return self ._findoffset_loc (width , height , xdescent , ydescent , renderer )
335+ self ._legend_box .set_offset (_findoffset_loc )
336+
337+ fontsize = renderer .points_to_pixels (self .fontsize )
324338
325339 # if mode == fill, set the width of the legend_box to the
326340 # width of the paret (minus pads)
327341 if self ._mode in ["expand" ]:
328- pad = 2 * (self .borderaxespad + self .borderpad )* self . fontsize
342+ pad = 2 * (self .borderaxespad + self .borderpad )* fontsize
329343 self ._legend_box .set_width (self .parent .bbox .width - pad )
330344
331345 if self ._drawFrame :
@@ -334,6 +348,8 @@ def draw(self, renderer):
334348 self .legendPatch .set_bounds (bbox .x0 , bbox .y0 ,
335349 bbox .width , bbox .height )
336350
351+ self .legendPatch .set_mutation_scale (fontsize )
352+
337353 if self .shadow :
338354 shadow = Shadow (self .legendPatch , 2 , - 2 )
339355 shadow .draw (renderer )
@@ -353,14 +369,19 @@ def _approx_text_height(self):
353369 return self .fontsize / 72.0 * self .figure .dpi
354370
355371
356- def _init_legend_box (self , handles , labels ):
372+ def _init_legend_box (self , handles , labels , renderer = None ):
357373 """
358374 Initiallize the legend_box. The legend_box is an instance of
359375 the OffsetBox, which is packed with legend handles and
360376 texts. Once packed, their location is calculated during the
361377 drawing time.
362378 """
363379
380+ if renderer is None :
381+ fontsize = self .fontsize
382+ else :
383+ fontsize = renderer .points_to_pixels (self .fontsize )
384+
364385 # legend_box is a HPacker, horizontally packed with
365386 # columns. Each column is a VPacker, vertically packed with
366387 # legend items. Each legend item is HPacker packed with
@@ -411,13 +432,13 @@ def _init_legend_box(self, handles, labels):
411432 if npoints > 1 :
412433 # we put some pad here to compensate the size of the
413434 # marker
414- xdata = np .linspace (0.3 * self . fontsize ,
415- (self .handlelength - 0.3 )* self . fontsize ,
435+ xdata = np .linspace (0.3 * fontsize ,
436+ (self .handlelength - 0.3 )* fontsize ,
416437 npoints )
417438 xdata_marker = xdata
418439 elif npoints == 1 :
419- xdata = np .linspace (0 , self .handlelength * self . fontsize , 2 )
420- xdata_marker = [0.5 * self .handlelength * self . fontsize ]
440+ xdata = np .linspace (0 , self .handlelength * fontsize , 2 )
441+ xdata_marker = [0.5 * self .handlelength * fontsize ]
421442
422443 if isinstance (handle , Line2D ):
423444 ydata = ((height - descent )/ 2. )* np .ones (xdata .shape , float )
@@ -445,7 +466,7 @@ def _init_legend_box(self, handles, labels):
445466
446467 elif isinstance (handle , Patch ):
447468 p = Rectangle (xy = (0. , 0. ),
448- width = self .handlelength * self . fontsize ,
469+ width = self .handlelength * fontsize ,
449470 height = (height - descent ),
450471 )
451472 p .update_from (handle )
@@ -499,7 +520,7 @@ def _init_legend_box(self, handles, labels):
499520 else :
500521 handle_list .append (None )
501522
502- handlebox = DrawingArea (width = self .handlelength * self . fontsize ,
523+ handlebox = DrawingArea (width = self .handlelength * fontsize ,
503524 height = height ,
504525 xdescent = 0. , ydescent = descent )
505526
@@ -527,15 +548,15 @@ def _init_legend_box(self, handles, labels):
527548 for i0 , di in largecol + smallcol :
528549 # pack handleBox and labelBox into itemBox
529550 itemBoxes = [HPacker (pad = 0 ,
530- sep = self .handletextpad * self . fontsize ,
551+ sep = self .handletextpad * fontsize ,
531552 children = [h , t ], align = "baseline" )
532553 for h , t in handle_label [i0 :i0 + di ]]
533554 # minimumdescent=False for the text of the last row of the column
534555 itemBoxes [- 1 ].get_children ()[1 ].set_minimumdescent (False )
535556
536557 # pack columnBox
537558 columnbox .append (VPacker (pad = 0 ,
538- sep = self .labelspacing * self . fontsize ,
559+ sep = self .labelspacing * fontsize ,
539560 align = "baseline" ,
540561 children = itemBoxes ))
541562
@@ -544,9 +565,9 @@ def _init_legend_box(self, handles, labels):
544565 else :
545566 mode = "fixed"
546567
547- sep = self .columnspacing * self . fontsize
568+ sep = self .columnspacing * fontsize
548569
549- self ._legend_box = HPacker (pad = self .borderpad * self . fontsize ,
570+ self ._legend_box = HPacker (pad = self .borderpad * fontsize ,
550571 sep = sep , align = "baseline" ,
551572 mode = mode ,
552573 children = columnbox )
@@ -627,7 +648,7 @@ def get_window_extent(self):
627648 return self .legendPatch .get_window_extent ()
628649
629650
630- def _get_anchored_bbox (self , loc , bbox , parentbbox ):
651+ def _get_anchored_bbox (self , loc , bbox , parentbbox , renderer ):
631652 """
632653 Place the *bbox* inside the *parentbbox* according to a given
633654 location code. Return the (x,y) coordinate of the bbox.
@@ -655,8 +676,9 @@ def _get_anchored_bbox(self, loc, bbox, parentbbox):
655676 C :"C" }
656677
657678 c = anchor_coefs [loc ]
658-
659- container = parentbbox .padded (- (self .borderaxespad ) * self .fontsize )
679+
680+ fontsize = renderer .points_to_pixels (self .fontsize )
681+ container = parentbbox .padded (- (self .borderaxespad ) * fontsize )
660682 anchored_box = bbox .anchored (c , container = container )
661683 return anchored_box .x0 , anchored_box .y0
662684
0 commit comments