pyplot.savefig fails with ValueError: cannot convert float NaN to integer

I think this is happening because I am plotting a gaussian that is very narrow compared to the x range. Still it seems failing here is bad behavior.

here is some code that reproduces the problem, taken from IPython notebook:

    %%matplotlib inline
    import matplotlib.pyplot as plt
    from scipy.stats import norm as scipy_stats_norm
    from math import sqrt

    plt.clf()
    fig, ax = plt.subplots()

    max_x = 1100  # this fails
    #max_x = 1000  # this works
    x = np.linspace(-max_x, max_x, 300) 

    mean = -0.0938
    stdev = sqrt(0.008812)
    ax.plot(x, scipy_stats_norm.pdf(x, loc=mean, scale=stdev), color='red', linewidth=2)

    ax.set_yticklabels([])
    ax.set_xlim(-max_x, max_x)

    filename = '/tmp/fail.png'
    plt.savefig(filename, bbox_inches='tight')
    plt.show()

Here’s the result:

  File "/opt/datasci-python/lib/python2.7/site-packages/matplotlib/pyplot.py", line 561, in savefig
    return fig.savefig(*args, **kwargs)
  File "/opt/datasci-python/lib/python2.7/site-packages/matplotlib/figure.py", line 1421, in savefig
    self.canvas.print_figure(*args, **kwargs)
  File "/opt/datasci-python/lib/python2.7/site-packages/matplotlib/backend_bases.py", line 2167, in print_figure
    **kwargs)
  File "/opt/datasci-python/lib/python2.7/site-packages/matplotlib/backends/backend_agg.py", line 505, in print_png
    FigureCanvasAgg.draw(self)
  File "/opt/datasci-python/lib/python2.7/site-packages/matplotlib/backends/backend_agg.py", line 451, in draw
    self.figure.draw(self.renderer)
  File "/opt/datasci-python/lib/python2.7/site-packages/matplotlib/artist.py", line 55, in draw_wrapper
    draw(artist, renderer, *args, **kwargs)
  File "/opt/datasci-python/lib/python2.7/site-packages/matplotlib/figure.py", line 1034, in draw
    func(*args)
  File "/opt/datasci-python/lib/python2.7/site-packages/matplotlib/artist.py", line 55, in draw_wrapper
    draw(artist, renderer, *args, **kwargs)
  File "/opt/datasci-python/lib/python2.7/site-packages/matplotlib/axes.py", line 2086, in draw
    a.draw(renderer)
  File "/opt/datasci-python/lib/python2.7/site-packages/matplotlib/artist.py", line 55, in draw_wrapper
    draw(artist, renderer, *args, **kwargs)
  File "/opt/datasci-python/lib/python2.7/site-packages/matplotlib/axis.py", line 1096, in draw
    tick.draw(renderer)
  File "/opt/datasci-python/lib/python2.7/site-packages/matplotlib/artist.py", line 55, in draw_wrapper
    draw(artist, renderer, *args, **kwargs)
  File "/opt/datasci-python/lib/python2.7/site-packages/matplotlib/axis.py", line 241, in draw
    self.label1.draw(renderer)
  File "/opt/datasci-python/lib/python2.7/site-packages/matplotlib/artist.py", line 55, in draw_wrapper
    draw(artist, renderer, *args, **kwargs)
  File "/opt/datasci-python/lib/python2.7/site-packages/matplotlib/text.py", line 598, in draw
    ismath=ismath, mtext=self)
  File "/opt/datasci-python/lib/python2.7/site-packages/matplotlib/backends/backend_agg.py", line 188, in draw_text
    font.get_image(), np.round(x - xd), np.round(y + yd) + 1, angle, gc)
ValueError: cannot convert float NaN to integer

Author: Fantashit

1 thought on “pyplot.savefig fails with ValueError: cannot convert float NaN to integer

  1. @dzieciou Your issue is that you create a text at a nan position. A minimal example would be

    import numpy as np
    import matplotlib.pyplot as plt
    
    plt.text(0, np.nan, "My Text")
    
    plt.savefig("barplot.png", bbox_inches="tight")
    plt.show()
    

    Workarounds:

    • Don’t try to position text at undefined positions.
    • Don’t use bbox_inches="tight" (the default for seaborn), but rather plot.savefig("barplot.png", bbox_inches=None).

Comments are closed.