FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

UserWarning thrown when all values are "bad", but not when only some are · Issue #18294 · matplotlib/matplotlib · GitHub

Repository navigation

UserWarning thrown when all values are "bad", but not when only some are #18294

Closed

Description

Bug report

Bug summary

If all values passed as colors are "bad" (np.nan), a warning is thrown from numpy about converting masked values to null. If some of the values are "bad", this does not happen.

Code for reproduction

import numpy as np, matplotlib.pyplot as plt

This does not warn:

c = np.full(10, np.nan)
c[0] = 1.
plt.scatter(np.ones(10), np.ones(10), c=c)
plt.show()

This does:

plt.scatter(np.ones(10), np.ones(10), c=np.full(10, np.nan))
plt.show()
<__array_function__ internals>:5: UserWarning: Warning: converting a masked element to nan.
/usr/local/lib/python3.8/site-packages/numpy/core/_asarray.py:85: UserWarning: Warning: converting a masked element to nan.
  return array(a, dtype, copy=False, order=order)

This is difficult to hide since since the warning is not thrown when the figure is being constructed, but when it is shown. Here's a full traceback to show this:

Full traceback
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python3.8/site-packages/matplotlib/pyplot.py", line 337, in show
    return _backend_mod.show(*args, **kwargs)
  File "/usr/local/lib/python3.8/site-packages/matplotlib/backend_bases.py", line 3544, in show
    cls.mainloop()
  File "/usr/local/lib/python3.8/site-packages/matplotlib/backends/backend_macosx.py", line 171, in mainloop
    _macosx.show()
  File "/usr/local/lib/python3.8/site-packages/matplotlib/backends/backend_macosx.py", line 61, in _draw
    self.figure.draw(renderer)
  File "/usr/local/lib/python3.8/site-packages/matplotlib/artist.py", line 41, in draw_wrapper
    return draw(artist, renderer, *args, **kwargs)
  File "/usr/local/lib/python3.8/site-packages/matplotlib/figure.py", line 1863, in draw
    mimage._draw_list_compositing_images(
  File "/usr/local/lib/python3.8/site-packages/matplotlib/image.py", line 131, in _draw_list_compositing_images
    a.draw(renderer)
  File "/usr/local/lib/python3.8/site-packages/matplotlib/artist.py", line 41, in draw_wrapper
    return draw(artist, renderer, *args, **kwargs)
  File "/usr/local/lib/python3.8/site-packages/matplotlib/cbook/deprecation.py", line 411, in wrapper
    return func(*inner_args, **inner_kwargs)
  File "/usr/local/lib/python3.8/site-packages/matplotlib/axes/_base.py", line 2748, in draw
    mimage._draw_list_compositing_images(renderer, self, artists)
  File "/usr/local/lib/python3.8/site-packages/matplotlib/image.py", line 131, in _draw_list_compositing_images
    a.draw(renderer)
  File "/usr/local/lib/python3.8/site-packages/matplotlib/artist.py", line 41, in draw_wrapper
    return draw(artist, renderer, *args, **kwargs)
  File "/usr/local/lib/python3.8/site-packages/matplotlib/collections.py", line 931, in draw
    Collection.draw(self, renderer)
  File "/usr/local/lib/python3.8/site-packages/matplotlib/artist.py", line 41, in draw_wrapper
    return draw(artist, renderer, *args, **kwargs)
  File "/usr/local/lib/python3.8/site-packages/matplotlib/collections.py", line 346, in draw
    self.update_scalarmappable()
  File "/usr/local/lib/python3.8/site-packages/matplotlib/collections.py", line 855, in update_scalarmappable
    self._facecolors = self.to_rgba(self._A, self._alpha)
  File "/usr/local/lib/python3.8/site-packages/matplotlib/cm.py", line 332, in to_rgba
    x = self.norm(x)
  File "/usr/local/lib/python3.8/site-packages/matplotlib/colors.py", line 1048, in __call__
    (vmin,), _ = self.process_value(self.vmin)
  File "/usr/local/lib/python3.8/site-packages/matplotlib/colors.py", line 1012, in process_value
    dtype = np.min_scalar_type(value)
  File "<__array_function__ internals>", line 5, in min_scalar_type
  File "/usr/local/lib/python3.8/site-packages/numpy/ma/core.py", line 4331, in __float__
    warnings.warn("Warning: converting a masked element to nan.", stacklevel=2)
  File "/usr/local/Cellar/python@3.8/3.8.5/Frameworks/Python.framework/Versions/3.8/lib/python3.8/warnings.py", line 109, in _showwarnmsg
    sw(msg.message, msg.category, msg.filename, msg.lineno,
  File "<stdin>", line 3, in warn_with_traceback
<__array_function__ internals>:5: UserWarning: Warning: converting a masked element to nan.
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python3.8/site-packages/matplotlib/pyplot.py", line 337, in show
    return _backend_mod.show(*args, **kwargs)
  File "/usr/local/lib/python3.8/site-packages/matplotlib/backend_bases.py", line 3544, in show
    cls.mainloop()
  File "/usr/local/lib/python3.8/site-packages/matplotlib/backends/backend_macosx.py", line 171, in mainloop
    _macosx.show()
  File "/usr/local/lib/python3.8/site-packages/matplotlib/backends/backend_macosx.py", line 61, in _draw
    self.figure.draw(renderer)
  File "/usr/local/lib/python3.8/site-packages/matplotlib/artist.py", line 41, in draw_wrapper
    return draw(artist, renderer, *args, **kwargs)
  File "/usr/local/lib/python3.8/site-packages/matplotlib/figure.py", line 1863, in draw
    mimage._draw_list_compositing_images(
  File "/usr/local/lib/python3.8/site-packages/matplotlib/image.py", line 131, in _draw_list_compositing_images
    a.draw(renderer)
  File "/usr/local/lib/python3.8/site-packages/matplotlib/artist.py", line 41, in draw_wrapper
    return draw(artist, renderer, *args, **kwargs)
  File "/usr/local/lib/python3.8/site-packages/matplotlib/cbook/deprecation.py", line 411, in wrapper
    return func(*inner_args, **inner_kwargs)
  File "/usr/local/lib/python3.8/site-packages/matplotlib/axes/_base.py", line 2748, in draw
    mimage._draw_list_compositing_images(renderer, self, artists)
  File "/usr/local/lib/python3.8/site-packages/matplotlib/image.py", line 131, in _draw_list_compositing_images
    a.draw(renderer)
  File "/usr/local/lib/python3.8/site-packages/matplotlib/artist.py", line 41, in draw_wrapper
    return draw(artist, renderer, *args, **kwargs)
  File "/usr/local/lib/python3.8/site-packages/matplotlib/collections.py", line 931, in draw
    Collection.draw(self, renderer)
  File "/usr/local/lib/python3.8/site-packages/matplotlib/artist.py", line 41, in draw_wrapper
    return draw(artist, renderer, *args, **kwargs)
  File "/usr/local/lib/python3.8/site-packages/matplotlib/collections.py", line 346, in draw
    self.update_scalarmappable()
  File "/usr/local/lib/python3.8/site-packages/matplotlib/collections.py", line 855, in update_scalarmappable
    self._facecolors = self.to_rgba(self._A, self._alpha)
  File "/usr/local/lib/python3.8/site-packages/matplotlib/cm.py", line 332, in to_rgba
    x = self.norm(x)
  File "/usr/local/lib/python3.8/site-packages/matplotlib/colors.py", line 1048, in __call__
    (vmin,), _ = self.process_value(self.vmin)
  File "/usr/local/lib/python3.8/site-packages/matplotlib/colors.py", line 1019, in process_value
    data = np.asarray(value)
  File "/usr/local/lib/python3.8/site-packages/numpy/core/_asarray.py", line 85, in asarray
    return array(a, dtype, copy=False, order=order)
  File "/usr/local/lib/python3.8/site-packages/numpy/ma/core.py", line 4331, in __float__
    warnings.warn("Warning: converting a masked element to nan.", stacklevel=2)
  File "/usr/local/Cellar/python@3.8/3.8.5/Frameworks/Python.framework/Versions/3.8/lib/python3.8/warnings.py", line 109, in _showwarnmsg
    sw(msg.message, msg.category, msg.filename, msg.lineno,
  File "<stdin>", line 3, in warn_with_traceback
/usr/local/lib/python3.8/site-packages/numpy/core/_asarray.py:85: UserWarning: Warning: converting a masked element to nan.
  return array(a, dtype, copy=False, order=order)

Expected outcome

I wouldn't expect the second to warn if the first doesn't.

Matplotlib version

  • Operating system: macOS
  • Matplotlib version: 3.3.1
  • Matplotlib backend (print(matplotlib.get_backend())): MacOSX and module://ipykernel.pylab.backend_inline
  • Python version: 3.8.5
  • Jupyter version (if applicable): Replicates in python repl, but also was happening in ipython and jupyter lab
  • Other libraries:

Installed with pip

Activity

  1. oscargus commented on Dec 15, 2022

    Member

    The warning comes from NumPy, so not sure there is much Matplotlib can do? I'll close this, but feel free to reopen if you have another opinion (and sorry for letting this go unnoticed for so long).

  2. ivirshup commented on Dec 15, 2022

    Author

    @oscargus, could matplotlib just catch the warning at plot time?

    I figure if the behavior is intentional from matplotlib, it would be nice if it didn't warn.

  3. oscargus commented on Dec 15, 2022

    Member

    Ahh, I should have checked what happens in latest Matplotlib. Now it actually breaks:

    ---------------------------------------------------------------------------
    StopIteration                             Traceback (most recent call last)
    <ipython-input-2-18fe43def80a> in <cell line: 1>()
    ----> 1 plt.scatter(np.ones(10), np.ones(10), c=np.full(10, np.nan))
    
    ~\matplotlib\lib\matplotlib\pyplot.py in scatter(x, y, s, c, marker, cmap, norm, vmin, vmax, alpha, linewidths, edgecolors, plotnonfinite, data, **kwargs)
       2820         vmin=None, vmax=None, alpha=None, linewidths=None, *,
       2821         edgecolors=None, plotnonfinite=False, data=None, **kwargs):
    -> 2822     __ret = gca().scatter(
       2823         x, y, s=s, c=c, marker=marker, cmap=cmap, norm=norm,
       2824         vmin=vmin, vmax=vmax, alpha=alpha, linewidths=linewidths,
    
    ~\matplotlib\lib\matplotlib\__init__.py in inner(ax, data, *args, **kwargs)
       1446     def inner(ax, *args, data=None, **kwargs):
       1447         if data is None:
    -> 1448             return func(ax, *map(sanitize_sequence, args), **kwargs)
       1449
       1450         bound = new_sig.bind(ax, *args, **kwargs)
    
    ~\matplotlib\lib\matplotlib\axes\_axes.py in scatter(self, x, y, s, c, marker, cmap, norm, vmin, vmax, alpha, linewidths, edgecolors, plotnonfinite, **kwargs)
       4590             orig_edgecolor = kwargs.get('edgecolor', None)
       4591         c, colors, edgecolors = \
    -> 4592             self._parse_scatter_color_args(
       4593                 c, edgecolors, kwargs, x.size,
       4594                 get_next_color_func=self._get_patches_for_fill.get_next_color)
    
    ~\matplotlib\lib\matplotlib\axes\_axes.py in _parse_scatter_color_args(c, edgecolors, kwargs, xsize, get_next_color_func)
       4388             isinstance(c, str)
       4389             or (np.iterable(c) and len(c) > 0
    -> 4390                 and isinstance(cbook._safe_first_finite(c), str)))
       4391
       4392         def invalid_shape_exception(csize, xsize):
    
    ~\matplotlib\lib\matplotlib\cbook\__init__.py in _safe_first_finite(obj, skip_nonfinite)
       1713                            "support generators as input")
       1714     else:
    -> 1715         return next(val for val in obj if safe_isfinite(val))
       1716
       1717
    
    StopIteration:
    

    Pinging @tacaswell who seems to have worked on _safe_first_finite most recently (although not sure when the issue arise).

    Regarding if it is intentional or not can be discussed. We don't raise the warning and if we catch it, it may lead to confusion. At least in the general case.

  4. added this to the v3.7.2 milestone on May 13, 2023
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


      Back | FazBrowse Home | New Git URL