Skip to content

Commit b6e044b

Browse files
committed
Only warn if best was used by default
1 parent f7be89e commit b6e044b

2 files changed

Lines changed: 17 additions & 6 deletions

File tree

‎lib/matplotlib/legend.py‎

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -485,9 +485,12 @@ def __init__(self, parent, handles, labels,
485485
self.parent = parent
486486

487487
if loc is None:
488+
self._loc_used_default = True
488489
loc = rcParams["legend.loc"]
489490
if not self.isaxes and loc in [0, 'best']:
490491
loc = 'upper right'
492+
else:
493+
self._loc_used_default = False
491494
if isinstance(loc, str):
492495
if loc not in self.codes:
493496
if self.isaxes:
@@ -568,7 +571,8 @@ def __init__(self, parent, handles, labels,
568571
else:
569572
self.get_frame().set_alpha(framealpha)
570573

571-
self._loc = loc
574+
self._set_loc(loc, is_initial_setting=True)
575+
572576
# figure out title fontsize:
573577
if title_fontsize is None:
574578
title_fontsize = rcParams['legend.title_fontsize']
@@ -588,10 +592,13 @@ def _set_artist_props(self, a):
588592

589593
a.set_transform(self.get_transform())
590594

591-
def _set_loc(self, loc):
595+
def _set_loc(self, loc, is_initial_setting=False):
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+
if not is_initial_setting:
600+
# User manually changed self._loc
601+
self._loc_used_default = False
595602
self._loc_real = loc
596603
self.stale = True
597604
self._legend_box.set_offset(self._findoffset)
@@ -1108,7 +1115,8 @@ def _find_best_position(self, width, height, renderer, consider=None):
11081115
assert self.isaxes
11091116

11101117
verts, bboxes, lines, offsets = self._auto_legend_data()
1111-
if len(verts) + len(bboxes) + len(lines) + len(offsets) > 500000:
1118+
if self._loc_used_default and verts.shape[0] > 200000:
1119+
# this size results in a 3+ second render time on a good machine
11121120
warnings.warn(
11131121
'Creating legend with loc="best" can be slow with large'
11141122
' amounts of data.'

‎lib/matplotlib/tests/test_legend.py‎

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -567,10 +567,13 @@ def test_alpha_handles():
567567

568568
def test_warn_big_data_best_loc():
569569
fig, ax = plt.subplots()
570-
ax.plot(np.arange(500001), label='Is this big data?')
570+
ax.plot(np.arange(200001), label='Is this big data?')
571571
with pytest.warns(UserWarning) as records:
572-
l = ax.legend(loc='best')
573-
l.draw(fig.canvas.get_renderer())
572+
l = ax.legend()
573+
# We need to call an internal set method tricking it into thinking
574+
# 'best' location was default.
575+
l._set_loc(l.codes['best'], is_initial_setting=True)
576+
fig.canvas.draw()
574577
# The _find_best_position method of Legend is called twice, duplicating
575578
# the warning message.
576579
assert len(records) == 2

0 commit comments

Comments
 (0)