Skip to content

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

Closed
@jessehersch

Description

@jessehersch

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

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions