Skip to content

imshow interpolation uses masked values #8012

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

Closed
dshean opened this issue Feb 3, 2017 · 10 comments
Closed

imshow interpolation uses masked values #8012

dshean opened this issue Feb 3, 2017 · 10 comments
Assignees
Labels
Release critical For bugs that make the library unusable (segfaults, incorrect plots, etc) and major regressions.
Milestone

Comments

@dshean
Copy link

dshean commented Feb 3, 2017

Bug report

I recently upgraded to v2.0.0 and noticed that the new imshow interpolation is not honoring masked values. This is likely related to the new edge handling, described here: http://matplotlib.org/examples/pylab_examples/image_interp.html

Code for reproduction

a = np.random.random((7,7))*1000
ndv = -9999.0
a[2:4,1:5] = ndv
am = np.ma.masked_equal(a, ndv)

plt.figure()
#Plot with no interpolation, looks fine
plt.imshow(am, interpolation='none')
plt.title("interpolation='none'")
#Plot with bilinear interpolation, the -9999.0 values are used 
plt.imshow(am, interpolation='bilinear')
plt.title("interpolation='bilinear'")

Actual outcome

none

bilinear

Expected outcome

The interpolation should ignore masked values as in previous versions.

Matplotlib version

  • Matplotlib v2.0.0, Python 2.7.13, OSX
  • installed from pip
@dshean dshean changed the title imshow interpolation ignores masked values imshow interpolation uses masked values Feb 3, 2017
@tacaswell tacaswell added this to the 2.0.1 (next bug fix release) milestone Feb 3, 2017
@tacaswell
Copy link
Member

It is honoring it, just in weird ways, will have a PR shortely:

so

@tacaswell
Copy link
Member

Note that in carves out a larger area because the region of screen pixels that get input from the masked data values is larger.

@tacaswell
Copy link
Member

The fix seems to break something else, will get to this tomorrow.

@tacaswell tacaswell added the Release critical For bugs that make the library unusable (segfaults, incorrect plots, etc) and major regressions. label Feb 3, 2017
@tacaswell tacaswell self-assigned this Feb 3, 2017
@tacaswell
Copy link
Member

This is way nastier than I expected 😞

@QuLogic
Copy link
Member

QuLogic commented Mar 13, 2017

@tacaswell Doesn't this still require some tweaks (cf. the last comment in #8024 and #8183)?

@tacaswell
Copy link
Member

Yes, on my to-do list for tonight

@gurraburra
Copy link

Note that in carves out a larger area because the region of screen pixels that get input from the masked data values is larger.

This seems still to be the case, would It be possible for interpolation to not change the size of the masked area?

@tacaswell
Copy link
Member

This seems still to be the case, would It be possible for interpolation to not change the size of the masked area?

No, because the way the interpolation works is by (effectively) convolving the input data with a fixed size kernel. Any place you evaluate the kernel that overlaps with a masked point also needs to be poisoned (e.g. if you drop a np.nan into any interpolation method then the output is always np.nan).

@gurraburra
Copy link

This seems still to be the case, would It be possible for interpolation to not change the size of the masked area?

No, because the way the interpolation works is by (effectively) convolving the input data with a fixed size kernel. Any place you evaluate the kernel that overlaps with a masked point also needs to be poisoned (e.g. if you drop a np.nan into any interpolation method then the output is always np.nan).

Yes I understand. My problem is not that I have np.nan, but simply that I want to mask some voxels from the image being shown. One solution could be to add a mask option to imshow, then the interpolation could be done before masking. Or another option would be to detect if the data is masked, then remove the mask (perhaps also replace masked values with zero), do the interpolation and then reapply the mask.

@tacaswell
Copy link
Member

If you want the masked pixel to be transparent, you are stuck, if you want to make it white, you can do something like

mask = np.ones((*data.shape, 4))  # RGBA of the right size
mask[~to_hide] = 0                # make the pixels you want to keep transparent
im_data = ax.imshow(data)
im_mask = ax.imshow(mask, zorder=im1.get_zorder() + 1, interpolation='nearest')

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Release critical For bugs that make the library unusable (segfaults, incorrect plots, etc) and major regressions.
Projects
None yet
Development

No branches or pull requests

4 participants