From c52dd214a8b34833c5d12a6464bc195900b1d0a5 Mon Sep 17 00:00:00 2001 From: Jody Klymak Date: Fri, 6 Oct 2017 13:08:13 -0700 Subject: [PATCH 1/2] In text, report to verbose and return instead of exception for non-finite x, y - Changed warning to verbose --- lib/matplotlib/text.py | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) 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 From 39a29243c6309a827398b031fcb63341fa41a9a5 Mon Sep 17 00:00:00 2001 From: Thomas A Caswell Date: Sat, 7 Oct 2017 15:18:54 -0400 Subject: [PATCH 2/2] TST: add test of text with non finite positions --- lib/matplotlib/tests/test_text.py | 7 +++++++ 1 file changed, 7 insertions(+) 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()