Skip to content

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

Merged
jklymak merged 1 commit into
matplotlib:masterfrom
anntzer:bestslow
Jul 26, 2019
Merged

Make slowness warning for legend(loc="best") more accurate.#14894
jklymak merged 1 commit into
matplotlib:masterfrom
anntzer:bestslow

Conversation

@anntzer

@anntzer anntzer commented Jul 26, 2019

Copy link
Copy Markdown
Contributor

... by actually timing the call duration. Locally I can best-locate
legends even with plots with hundreds of thousands of points basically
instantly, so the old warning was spurious.

The new test is obviously a bit brittle because it depends on how fast
the machine running it is. It's also slower than the test before
(intentionally, because now you actually need a slow-to-place legend
to trigger the warning).

The warning is only emitted after the legend has been placed, but that
seems fine -- if the best-placement is so slow that you ctrl-c the
process, you'll have a traceback anyways. Also, spawning a separate
thread to always emit the warning after exactly 5s will likely just make
things worse performance-wise on average.

The 5s delay is the same as used in font_manager.py.

The original warning went in as #12455.

PR Summary

PR Checklist

  • Has Pytest style unit tests
  • Code is Flake 8 compliant
  • New features are documented, with examples if plot related
  • Documentation is sphinx and numpydoc compliant
  • Added an entry to doc/users/next_whats_new/ if major new feature (follow instructions in README.rst there)
  • Documented in doc/api/api_changes.rst if API changed in a backward-incompatible way

@tacaswell tacaswell modified the milestones: v3.3.0, v3.2.0 Jul 26, 2019
Comment thread lib/matplotlib/legend.py Outdated
... by actually timing the call duration.  Locally I can best-locate
legends even with plots with hundreds of thousands of points basically
instantly, so the old warning was spurious.

The new test is obviously a bit brittle because it depends on how fast
the machine running it is.  It's also slower than the test before
(intentionally, because now you *actually* need a slow-to-place legend
to trigger the warning).

The warning is only emitted after the legend has been placed, but that
seems fine -- if the best-placement is so slow that you ctrl-c the
process, you'll have a traceback anyways.  Also, spawning a separate
thread to always emit the warning after exactly 5s will likely just make
things worse performance-wise on average.
Comment thread lib/matplotlib/legend.py

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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
Member

Choose a reason for hiding this comment

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

fair enough.

@jklymak

jklymak commented Jul 26, 2019

Copy link
Copy Markdown
Member

seems like a good idea

@jklymak jklymak merged commit 8630eb7 into matplotlib:master Jul 26, 2019
@anntzer anntzer deleted the bestslow branch July 27, 2019 07:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants