Skip to content

Commit c5ffd6c

Browse files
authored
Merge pull request matplotlib#30696 from timhoffm/fix-tightbox
FIX: Account for horizontal/vertical lines in tightbox
2 parents cd3685f + 4d40111 commit c5ffd6c

File tree

5 files changed

+36
-5
lines changed

5 files changed

+36
-5
lines changed

lib/matplotlib/axes/_base.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4632,9 +4632,7 @@ def get_tightbbox(self, renderer=None, *, call_axes_locator=True,
46324632

46334633
for a in bbox_artists:
46344634
bbox = a.get_tightbbox(renderer)
4635-
if (bbox is not None
4636-
and 0 < bbox.width < np.inf
4637-
and 0 < bbox.height < np.inf):
4635+
if bbox is not None and bbox._is_finite():
46384636
bb.append(bbox)
46394637
return mtransforms.Bbox.union(
46404638
[b for b in bb if b.width != 0 or b.height != 0])

lib/matplotlib/axis.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1383,8 +1383,7 @@ def get_tightbbox(self, renderer=None, *, for_layout_only=False):
13831383
bb.y0 = (bb.y0 + bb.y1) / 2 - 0.5
13841384
bb.y1 = bb.y0 + 1.0
13851385
bboxes.append(bb)
1386-
bboxes = [b for b in bboxes
1387-
if 0 < b.width < np.inf and 0 < b.height < np.inf]
1386+
bboxes = [b for b in bboxes if b._is_finite()]
13881387
if bboxes:
13891388
return mtransforms.Bbox.union(bboxes)
13901389
else:

lib/matplotlib/tests/test_transforms.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -835,6 +835,16 @@ def assert_bbox_eq(bbox1, bbox2):
835835
assert_array_equal(bbox1.bounds, bbox2.bounds)
836836

837837

838+
def test_bbox_is_finite():
839+
assert not Bbox([(1, 1), (1, 1)])._is_finite()
840+
assert not Bbox([(0, 0), (np.inf, 1)])._is_finite()
841+
assert not Bbox([(-np.inf, 0), (2, 2)])._is_finite()
842+
assert not Bbox([(np.nan, 0), (2, 2)])._is_finite()
843+
assert Bbox([(0, 0), (0, 2)])._is_finite()
844+
assert Bbox([(0, 0), (2, 0)])._is_finite()
845+
assert Bbox([(0, 0), (1, 2)])._is_finite()
846+
847+
838848
def test_bbox_frozen_copies_minpos():
839849
bbox = mtransforms.Bbox.from_extents(0.0, 0.0, 1.0, 1.0, minpos=1.0)
840850
frozen = bbox.frozen()

lib/matplotlib/transforms.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -377,6 +377,29 @@ def extents(self):
377377
def get_points(self):
378378
raise NotImplementedError
379379

380+
def _is_finite(self):
381+
"""
382+
Return whether the bounding box is finite and not degenerate to a
383+
single point.
384+
385+
We count the box as finite if neither width nor height are infinite
386+
and at least one direction is non-zero; i.e. a point is not finite,
387+
but a horizontal or vertical line is.
388+
389+
.. versionadded:: 3.11
390+
391+
Notes
392+
-----
393+
We keep this private for now because concise naming is hard and
394+
because we are not sure how universal the concept is. It is
395+
currently used only for filtering bboxes to be included in
396+
tightbbox calculation, but I'm unsure whether single points
397+
should be included there as well.
398+
"""
399+
width = self.width
400+
height = self.height
401+
return (width > 0 or height > 0) and width < np.inf and height < np.inf
402+
380403
def containsx(self, x):
381404
"""
382405
Return whether *x* is in the closed (:attr:`x0`, :attr:`x1`) interval.

lib/matplotlib/transforms.pyi

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ class BboxBase(TransformNode):
6565
@property
6666
def extents(self) -> tuple[float, float, float, float]: ...
6767
def get_points(self) -> np.ndarray: ...
68+
def _is_finite(self) -> bool: ...
6869
def containsx(self, x: float) -> bool: ...
6970
def containsy(self, y: float) -> bool: ...
7071
def contains(self, x: float, y: float) -> bool: ...

0 commit comments

Comments
 (0)