diff --git a/lib/matplotlib/axes/_base.py b/lib/matplotlib/axes/_base.py index e6b368d0117a..6981cbef280c 100644 --- a/lib/matplotlib/axes/_base.py +++ b/lib/matplotlib/axes/_base.py @@ -4259,7 +4259,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): + if (bbox is not None and + (bbox.width != 0 or bbox.height != 0) and + np.isfinite(bbox.width) and np.isfinite(bbox.height)): bb.append(bbox) _bbox = mtransforms.Bbox.union( diff --git a/lib/matplotlib/tests/test_axes.py b/lib/matplotlib/tests/test_axes.py index b4879e3b208a..08bff9272930 100644 --- a/lib/matplotlib/tests/test_axes.py +++ b/lib/matplotlib/tests/test_axes.py @@ -5849,3 +5849,10 @@ class DummySubplot(matplotlib.axes.SubplotBase, Dummy): FactoryDummySubplot = matplotlib.axes.subplot_class_factory(Dummy) assert DummySubplot is FactoryDummySubplot + + +def test_gettightbbox_ignoreNaN(): + fig, ax = plt.subplots() + t = ax.text(np.NaN, 1, 'Boo') + renderer = fig.canvas.get_renderer() + np.testing.assert_allclose(ax.get_tightbbox(renderer).width, 532.444444)