@@ -484,6 +484,7 @@ def __init__(self, parent, handles, labels,
484
484
raise TypeError ("Legend needs either Axes or Figure as parent" )
485
485
self .parent = parent
486
486
487
+ self ._loc_used_default = loc is None
487
488
if loc is None :
488
489
loc = rcParams ["legend.loc" ]
489
490
if not self .isaxes and loc in [0 , 'best' ]:
@@ -568,7 +569,10 @@ def __init__(self, parent, handles, labels,
568
569
else :
569
570
self .get_frame ().set_alpha (framealpha )
570
571
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
+
572
576
# figure out title fontsize:
573
577
if title_fontsize is None :
574
578
title_fontsize = rcParams ['legend.title_fontsize' ]
@@ -592,6 +596,7 @@ def _set_loc(self, loc):
592
596
# find_offset function will be provided to _legend_box and
593
597
# _legend_box will draw itself at the location of the return
594
598
# value of the find_offset.
599
+ self ._loc_used_default = False
595
600
self ._loc_real = loc
596
601
self .stale = True
597
602
self ._legend_box .set_offset (self ._findoffset )
@@ -1108,6 +1113,12 @@ def _find_best_position(self, width, height, renderer, consider=None):
1108
1113
assert self .isaxes
1109
1114
1110
1115
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
+ )
1111
1122
1112
1123
bbox = Bbox .from_bounds (0 , 0 , width , height )
1113
1124
if consider is None :
0 commit comments