Skip to content

Simplify check for tight-bbox finiteness.#13427

Merged
tacaswell merged 1 commit into
matplotlib:masterfrom
anntzer:isfinite
Feb 18, 2019
Merged

Simplify check for tight-bbox finiteness.#13427
tacaswell merged 1 commit into
matplotlib:masterfrom
anntzer:isfinite

Conversation

@anntzer

@anntzer anntzer commented Feb 13, 2019

Copy link
Copy Markdown
Contributor

0 < bbox.width < np.inf is as explicit as bbox.width != 0 and np.isfinite(bbox.width), shorter, and faster (not that it is a
bottleneck, though). (It works because bboxes are oriented to have
nonnegative widths and heights.)

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

Comment thread lib/matplotlib/axis.py

for a in [self.label, self.offsetText]:
bbox = a.get_window_extent(renderer)
if (np.isfinite(bbox.width) and np.isfinite(bbox.height) and

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.

this finiteness check is redundant with the one below

timhoffm
timhoffm previously approved these changes Feb 13, 2019
Comment thread lib/matplotlib/axes/_base.py Outdated
if (bbox is not None and
(bbox.width != 0 or bbox.height != 0) and
np.isfinite(bbox.width) and np.isfinite(bbox.height)):
0 < bbox.width < np.inf and 0 < bbox.height < np.inf):

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.

Optional, but might be easier to read when wrapping the height check to a separate line.

Comment thread lib/matplotlib/axis.py Outdated
@@ -1152,20 +1152,15 @@ def get_tightbbox(self, renderer):
self.offsetText.set_text(self.major.formatter.get_offset())

bb = []

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.

While we're at it, could be renamed to bboxes. Optional.

@anntzer

anntzer commented Feb 14, 2019

Copy link
Copy Markdown
Contributor Author

both comments handled

@jklymak

jklymak commented Feb 14, 2019

Copy link
Copy Markdown
Member

Is it worth doing bboxes = cbook.valid_bboxes(bboxes)?

@tacaswell

Copy link
Copy Markdown
Member

The values can be np.nan and

In [33]: np.nan < np.inf
Out[33]: False

See #13426

@timhoffm timhoffm dismissed their stale review February 14, 2019 05:53

Does not work.

@anntzer

anntzer commented Feb 14, 2019

Copy link
Copy Markdown
Contributor Author

But... that's the point here: these are places that check width != 0 and isfinite(width) so return False for nan, and 0 < width < inf also return False for nan.
(in fact I noticed this while reviewing #13426)

`0 < bbox.width < np.inf` is as explicit as `bbox.width != 0 and
np.isfinite(bbox.width)`, shorter, and faster (not that it is a
bottleneck, though).  (It works because bboxes are oriented to have
nonnegative widths and heights.)
@tacaswell

Copy link
Copy Markdown
Member

🐑 Sorry, was sleepy and got confused about the polarity we were looking at here.

@tacaswell tacaswell added this to the v3.1.0 milestone Feb 14, 2019
@tacaswell tacaswell merged commit a33422b into matplotlib:master Feb 18, 2019
@tacaswell

Copy link
Copy Markdown
Member

Thanks @anntzer

@anntzer anntzer deleted the isfinite branch February 18, 2019 19:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants