From c5a1c480ba5b5c11d52f32c7de4ce42852867fdf Mon Sep 17 00:00:00 2001 From: Antony Lee Date: Sat, 7 Oct 2017 12:56:39 -0700 Subject: [PATCH] Backport PR #9295: In text, warn and return instead of raise exception for non-finite x, y --- lib/matplotlib/tests/test_text.py | 7 +++++++ lib/matplotlib/text.py | 9 ++++++--- 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/lib/matplotlib/tests/test_text.py b/lib/matplotlib/tests/test_text.py index 7864389c96a5..01d692f76f61 100644 --- a/lib/matplotlib/tests/test_text.py +++ b/lib/matplotlib/tests/test_text.py @@ -441,3 +441,10 @@ def test_two_2line_texts(spacing1, spacing2): assert box1.height == box2.height else: assert box1.height != box2.height + + +def test_nonfinite_pos(): + fig, ax = plt.subplots() + ax.text(0, np.nan, 'nan') + ax.text(np.inf, 0, 'inf') + fig.canvas.draw() diff --git a/lib/matplotlib/text.py b/lib/matplotlib/text.py index c738f945f30e..9627f146cb4b 100644 --- a/lib/matplotlib/text.py +++ b/lib/matplotlib/text.py @@ -20,7 +20,7 @@ import matplotlib.artist as artist from matplotlib.artist import Artist from matplotlib.cbook import maxdict -from matplotlib import docstring +from matplotlib import docstring, verbose from matplotlib.font_manager import FontProperties from matplotlib.patches import FancyBboxPatch from matplotlib.patches import FancyArrowPatch, Rectangle @@ -759,9 +759,12 @@ def draw(self, renderer): # position in Text, and dash position in TextWithDash: posx = float(textobj.convert_xunits(textobj._x)) posy = float(textobj.convert_yunits(textobj._y)) - if not np.isfinite(posx) or not np.isfinite(posy): - raise ValueError("posx and posy should be finite values") posx, posy = trans.transform_point((posx, posy)) + if not np.isfinite(posx) or not np.isfinite(posy): + verbose.report("x and y are not finite values for text " + "string '{}'. Not rendering " + "text.".format(self.get_text()), 'helpful') + return canvasw, canvash = renderer.get_canvas_width_height() # draw the FancyBboxPatch