Skip to content
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

imshow no longer correctly handles 'bad' (nan) values #6069

Closed
1 task
tacaswell opened this issue Feb 28, 2016 · 10 comments
Closed
1 task

imshow no longer correctly handles 'bad' (nan) values #6069

tacaswell opened this issue Feb 28, 2016 · 10 comments
Assignees
Milestone

Comments

@tacaswell
Copy link
Member

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.
    so

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.
@tacaswell tacaswell added this to the 2.0 (style change major release) milestone Feb 28, 2016
@tacaswell
Copy link
Member Author

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.

tacaswell added a commit to tacaswell/matplotlib that referenced this issue Feb 28, 2016
The way that color mapping used to work was:

 - mask invalid
 - normalize
 - cmap (which uses the mask information fill in 'bad' values)
 - interpolate

The new order is:

 - mask invalid
 - interpolate (converting mask -> nan)
 - normalize (which passes through nan)
 - cmap

which results in cmap seeing the `nan` which it seems to map to
0.

This does the masking on the interpolated array just prior to
normalizing and removes it on the way in.

Fixes matplotlib#6069
@tacaswell tacaswell self-assigned this Feb 28, 2016
@mdboom mdboom assigned mdboom and tacaswell and unassigned tacaswell and mdboom Feb 29, 2016
@mdboom
Copy link
Member

mdboom commented Feb 29, 2016

Lemme know if you need help with this one.

@mdboom mdboom assigned mdboom and unassigned tacaswell Mar 2, 2016
tacaswell added a commit that referenced this issue Mar 4, 2016
Fix #6069.  Handle image masks correctly
tacaswell added a commit that referenced this issue Mar 4, 2016
Fix #6069.  Handle image masks correctly
@mankoff
Copy link

mankoff commented May 17, 2016

I think this bug still exists w/ RGB images. An MWE based on the test_image in the above fix.

fig, (ax1, ax2) = plt.subplots(1, 2)

foo = np.ones((5,5))
foo[1:2, 1:2] = np.nan
ax1.imshow(A)

bar = np.zeros((5,5,3))
bar[:,:,0] = 1
bar[:,:,1] = 0
bar[:,:,2] = 0
bar[1:2,1:2,:] = np.nan
ax2.imshow(bar)

screen shot 2016-05-17 at 16 14 08

@tacaswell
Copy link
Member Author

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

mankoff commented 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
Copy link
Member Author

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
Copy link
Member

efiring commented May 18, 2016

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.

@efiring
Copy link
Member

efiring commented 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 to imshow.

@benjimin
Copy link

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.

@tacaswell
Copy link
Member Author

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.

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

No branches or pull requests

5 participants