diff --git a/lib/matplotlib/artist.py b/lib/matplotlib/artist.py index bf096888ea66..6971a67215bf 100644 --- a/lib/matplotlib/artist.py +++ b/lib/matplotlib/artist.py @@ -232,6 +232,9 @@ def stale(self, val): def get_window_extent(self, renderer): """ Get the axes bounding box in display space. + + The bounding box' width and height are nonnegative. + Subclasses should override for inclusion in the bounding box "tight" calculation. Default is to return an empty bounding box at 0, 0. diff --git a/lib/matplotlib/axes/_base.py b/lib/matplotlib/axes/_base.py index 9515e03e5d75..e15e061d5ff4 100644 --- a/lib/matplotlib/axes/_base.py +++ b/lib/matplotlib/axes/_base.py @@ -4334,9 +4334,9 @@ def get_tightbbox(self, renderer, call_axes_locator=True, for a in bbox_artists: bbox = a.get_tightbbox(renderer) - if (bbox is not None and - (bbox.width != 0 or bbox.height != 0) and - np.isfinite(bbox.width) and np.isfinite(bbox.height)): + if (bbox is not None + and 0 < bbox.width < np.inf + and 0 < bbox.height < np.inf): bb.append(bbox) _bbox = mtransforms.Bbox.union( diff --git a/lib/matplotlib/axis.py b/lib/matplotlib/axis.py index e751176c8267..90f0dce3a405 100644 --- a/lib/matplotlib/axis.py +++ b/lib/matplotlib/axis.py @@ -1151,21 +1151,17 @@ def get_tightbbox(self, renderer): self._update_offset_text_position(ticklabelBoxes, ticklabelBoxes2) self.offsetText.set_text(self.major.formatter.get_offset()) - bb = [] - - for a in [self.label, self.offsetText]: - bbox = a.get_window_extent(renderer) - if (np.isfinite(bbox.width) and np.isfinite(bbox.height) and - a.get_visible()): - bb.append(bbox) - bb.extend(ticklabelBoxes) - bb.extend(ticklabelBoxes2) - bb = [b for b in bb if ((b.width != 0 or b.height != 0) and - np.isfinite(b.width) and - np.isfinite(b.height))] - if bb: - _bbox = mtransforms.Bbox.union(bb) - return _bbox + bboxes = [ + *(a.get_window_extent(renderer) + for a in [self.label, self.offsetText] + if a.get_visible()), + *ticklabelBoxes, + *ticklabelBoxes2, + ] + bboxes = [b for b in bboxes + if 0 < b.width < np.inf and 0 < b.height < np.inf] + if bboxes: + return mtransforms.Bbox.union(bboxes) else: return None