Skip to content

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

Closed

Description

@tacaswell

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.

Activity

  1. added this to the milestone on Feb 28, 2016
  2. tacaswell commented on Feb 28, 2016

    @tacaswell
    MemberAuthor

    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.

  3. added a commit that references this issue on Feb 28, 2016
    5a4bd76
  4. self-assigned this
    on Feb 28, 2016
  5. mdboom commented on Feb 29, 2016

    @mdboom
    Member

    Lemme know if you need help with this one.

  6. added a commit that references this issue on Mar 2, 2016
    eda0912
  7. assigned and unassigned on Mar 2, 2016
  8. added a commit that references this issue on Mar 4, 2016
    5c5a287
  9. added a commit that references this issue on Mar 4, 2016
    1302606
  10. mankoff commented on May 17, 2016

    @mankoff

    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

  11. tacaswell commented on May 17, 2016

    @tacaswell
    MemberAuthor

    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?

  12. mankoff commented on May 17, 2016

    @mankoff

    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?

  13. tacaswell commented on May 17, 2016

    @tacaswell
    MemberAuthor

    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?

  14. efiring commented on May 18, 2016

    @efiring
    Member

    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.

  15. efiring commented on May 18, 2016

    @efiring
    Member

    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.

  16. benjimin commented on Jul 19, 2017

    @benjimin

    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.

  17. tacaswell commented on Jul 19, 2017

    @tacaswell
    MemberAuthor

    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

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions