10000 Merge pull request #13426 from tacaswell/fix_nan_only_tightbbox · matplotlib/matplotlib@6701206 · GitHub
[go: up one dir, main page]

Skip to content

Commit 6701206

Browse files
authored
Merge pull request #13426 from tacaswell/fix_nan_only_tightbbox
FIX: bbox_inches='tight' with only non-finite bounding boxes
2 parents 3306021 + 0302451 commit 6701206

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

lib/matplotlib/figure.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2366,11 +2366,14 @@ def get_tightbbox(self, renderer, bbox_extra_artists=None):
23662366
except TypeError:
23672367
bbox = ax.get_tightbbox(renderer)
23682368
bb.append(bbox)
2369+
bb = [b for b in bb
2370+
if (np.isfinite(b.width) and np.isfinite(b.height)
2371+
and (b.width != 0 or b.height != 0))]
23692372

23702373
if len(bb) == 0:
23712374
return self.bbox_inches
23722375

2373-
_bbox = Bbox.union([b for b in bb if b.width != 0 or b.height != 0])
2376+
_bbox = Bbox.union(bb)
23742377

23752378
bbox_inches = TransformedBbox(_bbox,
23762379
Affine2D().scale(1. / self.dpi))

lib/matplotlib/tests/test_bbox_tight.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import numpy as np
2+
from io import BytesIO
23

34
from matplotlib.testing.decorators import image_comparison
45
import matplotlib.pyplot as plt
@@ -86,3 +87,12 @@ def test_bbox_inches_tight_raster():
8687
fig = plt.figure()
8788
ax = fig.add_subplot(111)
8889
ax.plot([1.0, 2.0], rasterized=True)
90+
91+
92+
def test_only_on_non_finite_bbox():
93+
94+
fig, ax = plt.subplots()
95+
ax.annotate("", xy=(0, float('nan')))
96+
ax.set_axis_off()
97+
# we only need to test that it does not error out on save
98+
fig.savefig(BytesIO(), bbox_inches='tight', format='png')

0 commit comments

Comments
 (0)
0