Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wilsonmr opened this issue Jan 24, 2019 · 2 comments · Fixed by #13426
Closed

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

wilsonmr opened this issue Jan 24, 2019 · 2 comments · Fixed by #13426
Milestone

Comments

@wilsonmr
Copy link

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)

@tacaswell
Copy link
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.

@wilsonmr
Copy link
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

tacaswell added a commit to tacaswell/matplotlib that referenced this issue Feb 18, 2019
Zaharid pushed a commit to NNPDF/reportengine that referenced this issue Apr 17, 2019
Zaharid added a commit to NNPDF/reportengine that referenced this issue Jul 29, 2019
…atplotlib/matplotlib#13276 is resolved"

This reverts commit 815f506.

This seems to no longer be needed and causes several figures to look
worse.
wilsonmr added a commit to NNPDF/reportengine that referenced this issue Jan 8, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants