Skip to content

Make slowness warning for legend(loc="best") more accurate. #14894

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 26, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 9 additions & 6 deletions lib/matplotlib/legend.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
"""

import logging
import time

import numpy as np

Expand Down Expand Up @@ -1112,13 +1113,9 @@ def _find_best_position(self, width, height, renderer, consider=None):
# should always hold because function is only called internally
assert self.isaxes

start_time = time.perf_counter()

verts, bboxes, lines, offsets = self._auto_legend_data()
if self._loc_used_default and verts.shape[0] > 200000:
# this size results in a 3+ second render time on a good machine
cbook._warn_external(
'Creating legend with loc="best" can be slow with large '
'amounts of data.'
)

bbox = Bbox.from_bounds(0, 0, width, height)
if consider is None:
Expand All @@ -1145,6 +1142,12 @@ def _find_best_position(self, width, height, renderer, consider=None):
candidates.append((badness, idx, (l, b)))

_, _, (l, b) = min(candidates)

if self._loc_used_default and time.perf_counter() - start_time > 1:
cbook._warn_external(
'Creating legend with loc="best" can be slow with large '
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

include the actual run time? Wish we could use the walrus here ;)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

would rather not, as if we do I'm fairly worried the next thing is that someone will ask for the warning threshold to be configurable and whatnot.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fair enough.

'amounts of data.')

return l, b

def contains(self, event):
Expand Down
22 changes: 13 additions & 9 deletions lib/matplotlib/tests/test_legend.py
Original file line number Diff line number Diff line change
Expand Up @@ -550,24 +550,28 @@ def test_alpha_handles():

def test_warn_big_data_best_loc():
fig, ax = plt.subplots()
ax.plot(np.arange(200001), label='Is this big data?')
fig.canvas.draw() # So that we can call draw_artist later.
for idx in range(1000):
ax.plot(np.arange(5000), label=idx)
with rc_context({'legend.loc': 'best'}):
legend = ax.legend()
with pytest.warns(UserWarning) as records:
with rc_context({'legend.loc': 'best'}):
ax.legend()
fig.canvas.draw()
fig.draw_artist(legend) # Don't bother drawing the lines -- it's slow.
# The _find_best_position method of Legend is called twice, duplicating
# the warning message.
assert len(records) == 2
for record in records:
assert str(record.message) == (
'Creating legend with loc="best" can be slow with large'
' amounts of data.')
'Creating legend with loc="best" can be slow with large '
'amounts of data.')


def test_no_warn_big_data_when_loc_specified():
fig, ax = plt.subplots()
ax.plot(np.arange(200001), label='Is this big data?')
fig.canvas.draw()
for idx in range(1000):
ax.plot(np.arange(5000), label=idx)
legend = ax.legend('best')
with pytest.warns(None) as records:
ax.legend('best')
fig.canvas.draw()
fig.draw_artist(legend)
assert len(records) == 0