Skip to content

Regression when calling annotate with nan values for the position #12648

Closed
@Zaharid

Description

@Zaharid

The following code:

import matplotlib.pyplot as plt
plt.annotate('X', (0, float('nan')))
plt.tight_layout()
plt.savefig('crash.png')

raises an exception when using matplotlib 3, including 3.0.1.
It works fine with matplotlib 2.

/tmp/mpl3/lib/python3.7/site-packages/numpy/core/fromnumeric.py:83: RuntimeWarning: invalid value encountered in reduce
  return ufunc.reduce(obj, axis, dtype, out, **passkwargs)
/tmp/mpl3/lib/python3.7/site-packages/numpy/core/_methods.py:32: RuntimeWarning: invalid value encountered in reduce
  return umr_minimum(a, axis, None, out, keepdims, initial)
/tmp/mpl3/lib/python3.7/site-packages/numpy/core/_methods.py:28: RuntimeWarning: invalid value encountered in reduce
  return umr_maximum(a, axis, None, out, keepdims, initial)
Traceback (most recent call last):
  File "mplbug.py", line 5, in <module>
    plt.savefig('crash.png')
  File "/tmp/mpl3/lib/python3.7/site-packages/matplotlib/pyplot.py", line 689, in savefig
    res = fig.savefig(*args, **kwargs)
  File "/tmp/mpl3/lib/python3.7/site-packages/matplotlib/figure.py", line 2094, in savefig
    self.canvas.print_figure(fname, **kwargs)
  File "/tmp/mpl3/lib/python3.7/site-packages/matplotlib/backends/backend_qt5agg.py", line 88, in print_figure
    super().print_figure(*args, **kwargs)
  File "/tmp/mpl3/lib/python3.7/site-packages/matplotlib/backend_bases.py", line 2075, in print_figure
    **kwargs)
  File "/tmp/mpl3/lib/python3.7/site-packages/matplotlib/backends/backend_agg.py", line 510, in print_png
    FigureCanvasAgg.draw(self)
  File "/tmp/mpl3/lib/python3.7/site-packages/matplotlib/backends/backend_agg.py", line 402, in draw
    self.figure.draw(self.renderer)
  File "/tmp/mpl3/lib/python3.7/site-packages/matplotlib/artist.py", line 50, in draw_wrapper
    return draw(artist, renderer, *args, **kwargs)
  File "/tmp/mpl3/lib/python3.7/site-packages/matplotlib/figure.py", line 1649, in draw
    renderer, self, artists, self.suppressComposite)
  File "/tmp/mpl3/lib/python3.7/site-packages/matplotlib/image.py", line 138, in _draw_list_compositing_images
    a.draw(renderer)
  File "/tmp/mpl3/lib/python3.7/site-packages/matplotlib/artist.py", line 50, in draw_wrapper
    return draw(artist, renderer, *args, **kwargs)
  File "/tmp/mpl3/lib/python3.7/site-packages/matplotlib/axes/_base.py", line 2610, in draw
    mimage._draw_list_compositing_images(renderer, self, artists)
  File "/tmp/mpl3/lib/python3.7/site-packages/matplotlib/image.py", line 138, in _draw_list_compositing_images
    a.draw(renderer)
  File "/tmp/mpl3/lib/python3.7/site-packages/matplotlib/artist.py", line 50, in draw_wrapper
    return draw(artist, renderer, *args, **kwargs)
  File "/tmp/mpl3/lib/python3.7/site-packages/matplotlib/axis.py", line 1185, in draw
    ticks_to_draw = self._update_ticks(renderer)
  File "/tmp/mpl3/lib/python3.7/site-packages/matplotlib/axis.py", line 1023, in _update_ticks
    tick_tups = list(self.iter_ticks())  # iter_ticks calls the locator
  File "/tmp/mpl3/lib/python3.7/site-packages/matplotlib/axis.py", line 967, in iter_ticks
    majorLocs = self.major.locator()
  File "/tmp/mpl3/lib/python3.7/site-packages/matplotlib/ticker.py", line 1985, in __call__
    return self.tick_values(vmin, vmax)
  File "/tmp/mpl3/lib/python3.7/site-packages/matplotlib/ticker.py", line 1993, in tick_values
    locs = self._raw_ticks(vmin, vmax)
  File "/tmp/mpl3/lib/python3.7/site-packages/matplotlib/ticker.py", line 1932, in _raw_ticks
    nbins = np.clip(self.axis.get_tick_space(),
  File "/tmp/mpl3/lib/python3.7/site-packages/matplotlib/axis.py", line 2159, in get_tick_space
    return int(np.floor(length / size))
ValueError: cannot convert float NaN to integer

Based on the traceback, the following issues might be related:

#10360 (likely this is a duplicate)
#11386
#6789

The snippet has been tested on brand new conda environments with the defaults
channel.

Matplotlib 3 env
$ conda create -p /tmp/mpl3 matplotlib=3 --yes
Solving environment: done

## Package Plan ##

  environment location: /tmp/mpl3

  added / updated specs: 
    - matplotlib=3


The following packages will be downloaded:

    package                    |            build
    ---------------------------|-----------------
    matplotlib-3.0.1           |   py37h5429711_0         6.6 MB

The following NEW packages will be INSTALLED:

    blas:             1.0-mkl                
    ca-certificates:  2018.03.07-0           
    certifi:          2018.10.15-py37_0      
    cycler:           0.10.0-py37_0          
    dbus:             1.13.2-h714fa37_1      
    expat:            2.2.6-he6710b0_0       
    fontconfig:       2.13.0-h9420a91_0      
    freetype:         2.9.1-h8a8886c_1       
    glib:             2.56.2-hd408876_0      
    gst-plugins-base: 1.14.0-hbbd80ab_1      
    gstreamer:        1.14.0-hb453b48_1      
    icu:              58.2-h9c2bf20_1        
    intel-openmp:     2019.0-118             
    jpeg:             9b-h024ee3a_2          
    kiwisolver:       1.0.1-py37hf484d3e_0   
    libedit:          3.1.20170329-h6b74fdf_2
    libffi:           3.2.1-hd88cf55_4       
    libgcc-ng:        8.2.0-hdf63c60_1       
    libgfortran-ng:   7.3.0-hdf63c60_0       
    libpng:           1.6.35-hbc83047_0      
    libstdcxx-ng:     8.2.0-hdf63c60_1       
    libuuid:          1.0.3-h1bed415_2       
    libxcb:           1.13-h1bed415_1        
    libxml2:          2.9.8-h26e45fe_1       
    matplotlib:       3.0.1-py37h5429711_0   
    mkl:              2019.0-118             
    mkl_fft:          1.0.6-py37h7dd41cf_0   
    mkl_random:       1.0.1-py37h4414c95_1   
    ncurses:          6.1-hf484d3e_0         
    numpy:            1.15.3-py37h1d66e8a_0  
    numpy-base:       1.15.3-py37h81de0dd_0  
    openssl:          1.0.2p-h14c3975_0      
    pcre:             8.42-h439df22_0        
    pip:              10.0.1-py37_0          
    pyparsing:        2.2.2-py37_0           
    pyqt:             5.9.2-py37h05f1152_2   
    python:           3.7.0-h6e4f718_3       
    python-dateutil:  2.7.3-py37_0           
    pytz:             2018.5-py37_0          
    qt:               5.9.6-h8703b6f_2       
    readline:         7.0-h7b6447c_5         
    setuptools:       40.4.3-py37_0          
    sip:              4.19.8-py37hf484d3e_0  
    six:              1.11.0-py37_1          
    sqlite:           3.25.2-h7b6447c_0      
    tk:               8.6.8-hbc83047_0       
    tornado:          5.1.1-py37h7b6447c_0   
    wheel:            0.32.2-py37_0          
    xz:               5.2.4-h14c3975_4       
    zlib:             1.2.11-ha838bed_2      


Downloading and Extracting Packages
matplotlib-3.0.1     | 6.6 MB    | ########################################################################### | 100% 
Preparing transaction: done
Verifying transaction: done
Executing transaction: done
#
# To activate this environment, use
#
#     $ conda activate /tmp/mpl3
#
# To deactivate an active environment, use
#
#     $ conda deactivate

Matplotlib 2 conda environment
$ conda create -p /tmp/mpl2 matplotlib=2 --yes                                                       (/tmp/mpl3) 
Solving environment: done

## Package Plan ##

  environment location: /tmp/mpl2

  added / updated specs: 
    - matplotlib=2


The following NEW packages will be INSTALLED:

    blas:             1.0-mkl                
    ca-certificates:  2018.03.07-0           
    certifi:          2018.10.15-py37_0      
    cycler:           0.10.0-py37_0          
    dbus:             1.13.2-h714fa37_1      
    expat:            2.2.6-he6710b0_0       
    fontconfig:       2.13.0-h9420a91_0      
    freetype:         2.9.1-h8a8886c_1       
    glib:             2.56.2-hd408876_0      
    gst-plugins-base: 1.14.0-hbbd80ab_1      
    gstreamer:        1.14.0-hb453b48_1      
    icu:              58.2-h9c2bf20_1        
    intel-openmp:     2019.0-118             
    jpeg:             9b-h024ee3a_2          
    kiwisolver:       1.0.1-py37hf484d3e_0   
    libedit:          3.1.20170329-h6b74fdf_2
    libffi:           3.2.1-hd88cf55_4       
    libgcc-ng:        8.2.0-hdf63c60_1       
    libgfortran-ng:   7.3.0-hdf63c60_0       
    libpng:           1.6.35-hbc83047_0      
    libstdcxx-ng:     8.2.0-hdf63c60_1       
    libuuid:          1.0.3-h1bed415_2       
    libxcb:           1.13-h1bed415_1        
    libxml2:          2.9.8-h26e45fe_1       
    matplotlib:       2.2.3-py37hb69df0a_0   
    mkl:              2019.0-118             
    mkl_fft:          1.0.6-py37h7dd41cf_0   
    mkl_random:       1.0.1-py37h4414c95_1   
    ncurses:          6.1-hf484d3e_0         
    numpy:            1.15.3-py37h1d66e8a_0  
    numpy-base:       1.15.3-py37h81de0dd_0  
    openssl:          1.0.2p-h14c3975_0      
    pcre:             8.42-h439df22_0        
    pip:              10.0.1-py37_0          
    pyparsing:        2.2.2-py37_0           
    pyqt:             5.9.2-py37h05f1152_2   
    python:           3.7.0-h6e4f718_3       
    python-dateutil:  2.7.3-py37_0           
    pytz:             2018.5-py37_0          
    qt:               5.9.6-h8703b6f_2       
    readline:         7.0-h7b6447c_5         
    setuptools:       40.4.3-py37_0          
    sip:              4.19.8-py37hf484d3e_0  
    six:              1.11.0-py37_1          
    sqlite:           3.25.2-h7b6447c_0      
    tk:               8.6.8-hbc83047_0       
    tornado:          5.1.1-py37h7b6447c_0   
    wheel:            0.32.2-py37_0          
    xz:               5.2.4-h14c3975_4       
    zlib:             1.2.11-ha838bed_2      

Preparing transaction: done
Verifying transaction: done
Executing transaction: done
#
# To activate this environment, use
#
#     $ conda activate /tmp/mpl2
#
# To deactivate an active environment, use
#
#     $ conda deactivate

The issue has been encountered in real code that expects MPL to deal with nan
values properly.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions