Repository navigation
Closed
Milestone
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 pltThis 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
Metadata
Metadata
Assignees
Labels
No labels
Activity
oscargus commentedon Dec 15, 2022
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).
ivirshup commentedon Dec 15, 2022
@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.
oscargus commentedon Dec 15, 2022
Ahh, I should have checked what happens in latest Matplotlib. Now it actually breaks:
Pinging @tacaswell who seems to have worked on
_safe_first_finitemost 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.