Skip to content

calling annotate with nan values for the position still gives error after 3.0.2 #13276

Closed

Description

@wilsonmr

Bug report

Bug summary

There was a previous issue #12648 which noted a regression in Matplotlib which meant calling annotate with a nan position caused an error when using the command plt.tight_layout(). This issue was then closed by #12651, however with Matplotlib 3.0.2 I still am recieving an error with the following code, which I think should be functionally the same:

import matplotlib.pyplot as plt

fig, ax = plt.subplots()

ax.annotate("", xy=(0, float('nan')))

# If axis is set on then a ValueError is thrown when calculating ticks
# If axis is set off then a RuntimeError: libpng signaled error is thrown
ax.set_axis_off()
fig.savefig("bar.png", bbox_inches='tight')

Actual outcome

libpng warning: Image width is zero in IHDR
libpng warning: Image height is zero in IHDR
libpng error: Invalid IHDR data
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/michael/conda/envs/nnpdf/lib/python3.7/site-packages/matplotlib/figure.py", line 2094, in savefig
    self.canvas.print_figure(fname, **kwargs)
  File "/Users/michael/conda/envs/nnpdf/lib/python3.7/site-packages/matplotlib/backend_bases.py", line 2075, in print_figure
    **kwargs)
  File "/Users/michael/conda/envs/nnpdf/lib/python3.7/site-packages/matplotlib/backends/backend_agg.py", line 523, in print_png
    self.figure.dpi, metadata=metadata)
RuntimeError: libpng signaled error

Expected outcome

I think this minimal example should just return an empty figure and ignore the annotation with NaN position. Maybe I'm mistaken but I would have thought it should be the same output as

plt.tight_layout()
plt.savefig("bar.png")

which does work as per the #12651

Matplotlib version

  • Operating system: OSX, but also tested on linux
  • Matplotlib version: 3.0.2
  • Matplotlib backend (print(matplotlib.get_backend())): TkAgg
  • Python version: 3.7.2

(conda installation)

Activity

  1. added this to the v3.0.3 milestone on Feb 13, 2019
  2. tacaswell commented on Feb 13, 2019

    @tacaswell
    Member

    We special case no artist, and handle the case of just one artist with an invalid bounding box, but fail on the case with only artists with invalid bounding boxes.

    I'll have a PR in shortly.

    Thanks for the extremely clear example (it is going to made into a test!).

    Could not reproduce the ValueError though.

  3. added a commit that references this issue on Feb 13, 2019
    1a4b6ee
  4. wilsonmr commented on Feb 13, 2019

    @wilsonmr
    Author

    Thanks for the reply! Perhaps the ValueError is dependent on numpy version, otherwise I'm not sure. I'm getting it with
    numpy 1.15.4 py37hacdab7b_0
    and numpy-base 1.15.4 py37h6575580_0
    Full traceback in case it's more instructive:

    >>> import matplotlib.pyplot as plt
    >>> fig, ax = plt.subplots()
    >>> ax.annotate("", xy=(0,float('nan')))
    Text(0, nan, '')
    >>> fig.savefig("foo.png", bbox_inches='tight')
    /Users/michael/conda/envs/mpl/lib/python3.7/site-packages/numpy/core/fromnumeric.py:83: RuntimeWarning: invalid value encountered in reduce
      return ufunc.reduce(obj, axis, dtype, out, **passkwargs)
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
      File "/Users/michael/conda/envs/mpl/lib/python3.7/site-packages/matplotlib/figure.py", line 2094, in savefig
        self.canvas.print_figure(fname, **kwargs)
      File "/Users/michael/conda/envs/mpl/lib/python3.7/site-packages/matplotlib/backend_bases.py", line 2075, in print_figure
        **kwargs)
      File "/Users/michael/conda/envs/mpl/lib/python3.7/site-packages/matplotlib/backends/backend_agg.py", line 510, in print_png
        FigureCanvasAgg.draw(self)
      File "/Users/michael/conda/envs/mpl/lib/python3.7/site-packages/matplotlib/backends/backend_agg.py", line 402, in draw
        self.figure.draw(self.renderer)
      File "/Users/michael/conda/envs/mpl/lib/python3.7/site-packages/matplotlib/artist.py", line 50, in draw_wrapper
        return draw(artist, renderer, *args, **kwargs)
      File "/Users/michael/conda/envs/mpl/lib/python3.7/site-packages/matplotlib/figure.py", line 1649, in draw
        renderer, self, artists, self.suppressComposite)
      File "/Users/michael/conda/envs/mpl/lib/python3.7/site-packages/matplotlib/image.py", line 138, in _draw_list_compositing_images
        a.draw(renderer)
      File "/Users/michael/conda/envs/mpl/lib/python3.7/site-packages/matplotlib/artist.py", line 50, in draw_wrapper
        return draw(artist, renderer, *args, **kwargs)
      File "/Users/michael/conda/envs/mpl/lib/python3.7/site-packages/matplotlib/axes/_base.py", line 2628, in draw
        mimage._draw_list_compositing_images(renderer, self, artists)
      File "/Users/michael/conda/envs/mpl/lib/python3.7/site-packages/matplotlib/image.py", line 138, in _draw_list_compositing_images
        a.draw(renderer)
      File "/Users/michael/conda/envs/mpl/lib/python3.7/site-packages/matplotlib/artist.py", line 50, in draw_wrapper
        return draw(artist, renderer, *args, **kwargs)
      File "/Users/michael/conda/envs/mpl/lib/python3.7/site-packages/matplotlib/axis.py", line 1185, in draw
        ticks_to_draw = self._update_ticks(renderer)
      File "/Users/michael/conda/envs/mpl/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 "/Users/michael/conda/envs/mpl/lib/python3.7/site-packages/matplotlib/axis.py", line 967, in iter_ticks
        majorLocs = self.major.locator()
      File "/Users/michael/conda/envs/mpl/lib/python3.7/site-packages/matplotlib/ticker.py", line 1985, in __call__
        return self.tick_values(vmin, vmax)
      File "/Users/michael/conda/envs/mpl/lib/python3.7/site-packages/matplotlib/ticker.py", line 1993, in tick_values
        locs = self._raw_ticks(vmin, vmax)
      File "/Users/michael/conda/envs/mpl/lib/python3.7/site-packages/matplotlib/ticker.py", line 1932, in _raw_ticks
        nbins = np.clip(self.axis.get_tick_space(),
      File "/Users/michael/conda/envs/mpl/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
    
  5. added a commit that references this issue on Feb 18, 2019
    0302451
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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