3333
3434from matplotlib import rcParams
3535from matplotlib .artist import Artist , allow_rasterization
36- from matplotlib .cbook import is_string_like , iterable , silent_list , is_hashable
36+ from matplotlib .cbook import is_string_like , silent_list , is_hashable
3737from matplotlib .font_manager import FontProperties
3838from matplotlib .lines import Line2D
3939from matplotlib .patches import Patch , Rectangle , Shadow , FancyBboxPatch
@@ -408,17 +408,6 @@ def _set_loc(self, loc):
408408 # find_offset function will be provided to _legend_box and
409409 # _legend_box will draw itself at the location of the return
410410 # value of the find_offset.
411- self ._loc_real = loc
412- if loc == 0 :
413- _findoffset = self ._findoffset_best
414- else :
415- _findoffset = self ._findoffset_loc
416-
417- # def findoffset(width, height, xdescent, ydescent):
418- # return _findoffset(width, height, xdescent, ydescent, renderer)
419-
420- self ._legend_box .set_offset (_findoffset )
421-
422411 self ._loc_real = loc
423412 self .stale = True
424413
@@ -427,24 +416,20 @@ def _get_loc(self):
427416
428417 _loc = property (_get_loc , _set_loc )
429418
430- def _findoffset_best (self , width , height , xdescent , ydescent , renderer ):
431- "Helper function to locate the legend at its best position"
432- ox , oy = self ._find_best_position (width , height , renderer )
433- return ox + xdescent , oy + ydescent
434-
435- def _findoffset_loc (self , width , height , xdescent , ydescent , renderer ):
436- "Helper function to locate the legend using the location code"
419+ def _findoffset (self , width , height , xdescent , ydescent , renderer ):
420+ "Helper function to locate the legend"
437421
438- if iterable (self ._loc ) and len (self ._loc ) == 2 :
439- # when loc is a tuple of axes(or figure) coordinates.
440- fx , fy = self ._loc
441- bbox = self .get_bbox_to_anchor ()
442- x , y = bbox .x0 + bbox .width * fx , bbox .y0 + bbox .height * fy
443- else :
422+ if self ._loc == 0 : # "best".
423+ x , y = self ._find_best_position (width , height , renderer )
424+ elif self ._loc in Legend .codes .values (): # Fixed location.
444425 bbox = Bbox .from_bounds (0 , 0 , width , height )
445426 x , y = self ._get_anchored_bbox (self ._loc , bbox ,
446427 self .get_bbox_to_anchor (),
447428 renderer )
429+ else : # Axes or figure coordinates.
430+ fx , fy = self ._loc
431+ bbox = self .get_bbox_to_anchor ()
432+ x , y = bbox .x0 + bbox .width * fx , bbox .y0 + bbox .height * fy
448433
449434 return x + xdescent , y + ydescent
450435
@@ -701,6 +686,7 @@ def _init_legend_box(self, handles, labels, markerfirst=True):
701686 children = [self ._legend_title_box ,
702687 self ._legend_handle_box ])
703688 self ._legend_box .set_figure (self .figure )
689+ self ._legend_box .set_offset (self ._findoffset )
704690 self .texts = text_list
705691 self .legendHandles = handle_list
706692
0 commit comments