From cd81f49cdb0e2882f3ba695bfe79eb671b3589d5 Mon Sep 17 00:00:00 2001 From: Thomas A Caswell Date: Mon, 18 Feb 2019 14:41:57 -0500 Subject: [PATCH] Backport PR #13426: FIX: bbox_inches='tight' with only non-finite bounding boxes --- lib/matplotlib/figure.py | 5 ++++- lib/matplotlib/tests/test_bbox_tight.py | 10 ++++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/lib/matplotlib/figure.py b/lib/matplotlib/figure.py index ae64ce6034ed..ed6baa055eb1 100644 --- a/lib/matplotlib/figure.py +++ b/lib/matplotlib/figure.py @@ -2281,11 +2281,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')