@@ -484,6 +484,7 @@ def __init__(self, parent, handles, labels,
484484 raise TypeError ("Legend needs either Axes or Figure as parent" )
485485 self .parent = parent
486486
487+ self ._loc_used_default = loc is None
487488 if loc is None :
488489 loc = rcParams ["legend.loc" ]
489490 if not self .isaxes and loc in [0 , 'best' ]:
@@ -568,7 +569,10 @@ def __init__(self, parent, handles, labels,
568569 else :
569570 self .get_frame ().set_alpha (framealpha )
570571
571- self ._loc = loc
572+ tmp = self ._loc_used_default
573+ self ._set_loc (loc )
574+ self ._loc_used_default = tmp # ignore changes done by _set_loc
575+
572576 # figure out title fontsize:
573577 if title_fontsize is None :
574578 title_fontsize = rcParams ['legend.title_fontsize' ]
@@ -592,6 +596,7 @@ def _set_loc(self, loc):
592596 # find_offset function will be provided to _legend_box and
593597 # _legend_box will draw itself at the location of the return
594598 # value of the find_offset.
599+ self ._loc_used_default = False
595600 self ._loc_real = loc
596601 self .stale = True
597602 self ._legend_box .set_offset (self ._findoffset )
@@ -1108,6 +1113,12 @@ def _find_best_position(self, width, height, renderer, consider=None):
11081113 assert self .isaxes
11091114
11101115 verts , bboxes , lines , offsets = self ._auto_legend_data ()
1116+ if self ._loc_used_default and verts .shape [0 ] > 200000 :
1117+ # this size results in a 3+ second render time on a good machine
1118+ cbook ._warn_external (
1119+ 'Creating legend with loc="best" can be slow with large '
1120+ 'amounts of data.'
1121+ )
11111122
11121123 bbox = Bbox .from_bounds (0 , 0 , width , height )
11131124 if consider is None :
0 commit comments