diff --git a/lib/matplotlib/figure.py b/lib/matplotlib/figure.py index f86c0c78936c..0872f10247e8 100644 --- a/lib/matplotlib/figure.py +++ b/lib/matplotlib/figure.py @@ -2366,11 +2366,14 @@ def get_tightbbox(self, renderer, bbox_extra_artists=None): except TypeError: bbox = ax.get_tightbbox(renderer) bb.append(bbox) + bb = [b for b in bb + if (np.isfinite(b.width) and np.isfinite(b.height) + and (b.width != 0 or b.height != 0))] if len(bb) == 0: return self.bbox_inches - _bbox = Bbox.union([b for b in bb if b.width != 0 or b.height != 0]) + _bbox = Bbox.union(bb) bbox_inches = TransformedBbox(_bbox, Affine2D().scale(1. / self.dpi)) diff --git a/lib/matplotlib/tests/test_bbox_tight.py b/lib/matplotlib/tests/test_bbox_tight.py index 46f37632e944..8c99c4572f76 100644 --- a/lib/matplotlib/tests/test_bbox_tight.py +++ b/lib/matplotlib/tests/test_bbox_tight.py @@ -1,4 +1,5 @@ import numpy as np +from io import BytesIO from matplotlib.testing.decorators import image_comparison import matplotlib.pyplot as plt @@ -86,3 +87,12 @@ def test_bbox_inches_tight_raster(): fig = plt.figure() ax = fig.add_subplot(111) ax.plot([1.0, 2.0], rasterized=True) + + +def test_only_on_non_finite_bbox(): + + fig, ax = plt.subplots() + ax.annotate("", xy=(0, float('nan'))) + ax.set_axis_off() + # we only need to test that it does not error out on save + fig.savefig(BytesIO(), bbox_inches='tight', format='png')