You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
imshow no longer correctly handles 'bad' (nan) values #6069
To help us understand and resolve your issue please check that you have provided
the information below.
[ v2.x ] Matplotlib version, Python version and Platform (Windows, OSX, Linux ...)
[ source ] How did you install Matplotlib and Python (pip, anaconda, from source ...)
If possible please supply a Short, Self Contained, Correct, Example
that demonstrates the issue i.e a small piece of code which reproduces the issue
and can be run with out any other (or as few as possible) external dependencies.
plt.imshow(np.ones((5, 5)) *np.nan)
If this is an image generation bug attach a screenshot demonstrating the issue.
Also got some user warnings
/home/tcaswell/.virtualenvs/dd_35/lib/python3.5/site-packages/numpy/ma/core.py:4089: UserWarning: Warning: converting a masked element to nan.
warnings.warn("Warning: converting a masked element to nan.")
/home/tcaswell/source/p/matplotlib/lib/matplotlib/colors.py:581: RuntimeWarning: invalid value encountered in less
cbook._putmask(xa, xa < 0.0, -1)
/home/tcaswell/source/p/matplotlib/lib/matplotlib/cm.py:275: RuntimeWarning: invalid value encountered in less
rgba[:, :, 3][x < 0.0] = 0
[ always ] If this is a regression (Used to work in an earlier version of Matplotlib), please
note where it used to work.
It looks like new image re-sampling code loses the mask that gets added on the way into AxesImage which then does not propagate properly through norm / color map.
This original bug in this issue with the new image code which is only on 2.x/master (not released yet).
What I mean by 'no color mapping' is that if you pass a (M, N, 3) or (M, N, 4) array to imshow it does not get run through the normalization / color mapping code path. The [0,1] -> R3 step has a mapping, plus three special colors 'over', 'under', and 'bad'.
I am actually not sure what to expect for an RGB/RGBA input with nans in it. Did this used to do something different? In any case, can you make a new issue?
Something might have gotten lost. We are using safe_masked_invalid in set_data, so I suspect that the mask was being handled prior to the big refactoring. What was being rendered in the masked locations, I don't know.
Contrary to my last comment, I don't think anything has been lost. Looking at v1.4.1, I don't see any indication in the code or the docstring that invalid or masked data has ever been supported for RGB or RGBA arrays. Even though they get passed through safe_masked_invalid, their mask is never checked or used. Maybe the thing to do is to warn if any values are actually masked, and then use .filled(0) to turn them back into ordinary ndarrays. Or raise an exception if any values are masked. I think it should remain the responsibility of the supplier of RGB or RGBA arrays to handle bad values before passing the arrays in to imshow.
I think it is a useful feature that regions of NaN appear like holes when visualised by imshow. Unfortunately (in matplotlib 2.0.0) imshow raises ValueError if the raster array does not contain any finite values whatsoever.
Activity
tacaswell commentedon Feb 28, 2016
It looks like new image re-sampling code loses the mask that gets added on the way into
AxesImagewhich then does not propagate properly through norm / color map.FIX: do invalid masking in to_rgba not set_data
mdboom commentedon Feb 29, 2016
Lemme know if you need help with this one.
Fix matplotlib#6069. Handle image masks correctly
Merge pull request #6099 from mdboom/masked-image
Merge pull request #6099 from mdboom/masked-image
mankoff commentedon May 17, 2016
I think this bug still exists w/ RGB images. An MWE based on the
test_imagein the above fix.tacaswell commentedon May 17, 2016
In the case of RGBA there is no color mapping. The black coming through is a bit odd.
@mankoff What version of mpl and what backend are you using?
mankoff commentedon May 17, 2016
mpl: 1.5.1
backend: Qt4Agg
OS: OS X.
I'm not sure what you mean by "no color mapping". Is it possible to have transparent values in an RGB displayed with imshow?
tacaswell commentedon May 17, 2016
This original bug in this issue with the new image code which is only on 2.x/master (not released yet).
What I mean by 'no color mapping' is that if you pass a (M, N, 3) or (M, N, 4) array to imshow it does not get run through the normalization / color mapping code path. The [0,1] -> R3 step has a mapping, plus three special colors 'over', 'under', and 'bad'.
I am actually not sure what to expect for an RGB/RGBA input with nans in it. Did this used to do something different? In any case, can you make a new issue?
efiring commentedon May 18, 2016
Something might have gotten lost. We are using
safe_masked_invalidinset_data, so I suspect that the mask was being handled prior to the big refactoring. What was being rendered in the masked locations, I don't know.efiring commentedon May 18, 2016
Contrary to my last comment, I don't think anything has been lost. Looking at v1.4.1, I don't see any indication in the code or the docstring that invalid or masked data has ever been supported for RGB or RGBA arrays. Even though they get passed through
safe_masked_invalid, their mask is never checked or used. Maybe the thing to do is to warn if any values are actually masked, and then use.filled(0)to turn them back into ordinary ndarrays. Or raise an exception if any values are masked. I think it should remain the responsibility of the supplier of RGB or RGBA arrays to handle bad values before passing the arrays in toimshow.benjimin commentedon Jul 19, 2017
I think it is a useful feature that regions of NaN appear like holes when visualised by
imshow. Unfortunately (in matplotlib 2.0.0)imshowraises ValueError if the raster array does not contain any finite values whatsoever.tacaswell commentedon Jul 19, 2017
Can you try with 2.0.2 or provide a MWE in a new issue?
I have a memory of fixing something like that recently.