Thanks to visit codestin.com
Credit goes to github.com

Skip to content

BUG: Fix all-masked imshow #18500

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

Merged
merged 1 commit into from
Sep 17, 2020
Merged

BUG: Fix all-masked imshow #18500

merged 1 commit into from
Sep 17, 2020

Conversation

larsoner
Copy link
Contributor

PR Summary

Deal with imshowing an array that is completely masked without emitting a warning. The added test/code emits 3 warnings on master, none of which are particularly informative. The test on master fails with:

lib/matplotlib/image.py:639: in draw
    im, l, b, trans = self.make_image(
lib/matplotlib/image.py:924: in make_image
    return self._make_image(self._A, bbox, transformed_bbox, clip,
lib/matplotlib/image.py:476: in _make_image
    vrange = np.array([vmin, vmax], dtype=scaled_dtype)
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 

self = masked

    def __float__(self):
        """
        Convert to float.
    
        """
        if self.size > 1:
            raise TypeError("Only length-1 arrays can be converted "
                            "to Python scalars")
        elif self._mask:
>           warnings.warn("Warning: converting a masked element to nan.", stacklevel=2)
E           UserWarning: Warning: converting a masked element to nan.

And on latest NumPy it also emits a warning later having to do with np.clip not liking np.nan values.

Alternatively, imshow with all data masked could emit a warning, but it seems like the existing code had a chance to do that but didn't (there is an existing if a_min is np.ma.masked on line 409) so I didn't implement it here.

PR Checklist

  • Has pytest style unit tests (and pytest passes).
  • Is Flake 8 compliant (run flake8 on changed files to check).
  • New features are documented, with examples if plot related.
  • Documentation is sphinx and numpydoc compliant (the docs should build without error).
  • Conforms to Matplotlib style conventions (install flake8-docstrings and pydocstyle<4 and run flake8 --docstring-convention=all).
  • New features have an entry in doc/users/next_whats_new/ (follow instructions in README.rst there).
  • API changes documented in doc/api/next_api_changes/ (follow instructions in README.rst there).

@tacaswell tacaswell added this to the v3.3.3 milestone Sep 16, 2020
@QuLogic QuLogic merged commit a4128a7 into matplotlib:master Sep 17, 2020
meeseeksmachine pushed a commit to meeseeksmachine/matplotlib that referenced this pull request Sep 17, 2020
@larsoner larsoner deleted the masked branch September 17, 2020 01:10
@larsoner
Copy link
Contributor Author

Thanks for the quick reviews!

QuLogic added a commit that referenced this pull request Sep 17, 2020
…500-on-v3.3.x

Backport PR #18500 on branch v3.3.x (BUG: Fix all-masked imshow)
data = np.full((2, 2), np.nan)
fig, ax = plt.subplots()
ax.imshow(data)
fig.canvas.draw_idle() # would emit a warning
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unless we run with warnings as error (which I'm pretty sure we don't) this test doesn't actually check that we don't issue warnings...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it failed on master before the other changes in this PR, and this suggested to me that you do:

https://github.com/matplotlib/matplotlib/blob/master/lib/matplotlib/testing/conftest.py#L20

But I can wrap this in a with pytest.warns(None) as w: ... assert len(w) == 0 if it'll help

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(see the traceback in the description, that was from the original test on master without the fixing-changes)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pytest does treat warnings as errors.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, I wasn't aware we had that turned on in conftest.py. I'm used to testing on MetPy where so many upstream dependencies have warnings at times that running with warnings is impractical. Apologies for the noise.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants