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

Fix problem with legend if data has NaN's [backport to 1.4.x] #3613

Merged
merged 1 commit into from Oct 5, 2014

Conversation

demotu
Copy link
Contributor

@demotu demotu commented Oct 3, 2014

If data has NaN's and the plot a legend with parameter 'best', matplotlib throws the following RuntimeWarning:

...\matplotlib\transforms.py:651: RuntimeWarning: invalid value encountered in sign
dx0 = np.sign(vertices[:, 0] - x0)
...\matplotlib\transforms.py:652: RuntimeWarning: invalid value encountered in sign
dy0 = np.sign(vertices[:, 1] - y0)
...\matplotlib\transforms.py:653: RuntimeWarning: invalid value encountered in sign
dx1 = np.sign(vertices[:, 0] - x1)
...\matplotlib\transforms.py:654: RuntimeWarning: invalid value encountered in sign
dy1 = np.sign(vertices[:, 1] - y1)

The following code reproduces this RuntimeWarning:
plt.plot([1,2,np.nan,4,5], label='test')
plt.legend(loc='best')

The solution is to check for NaN's in data and delete those rows, as suggested.

@tacaswell tacaswell added this to the v1.4.x milestone Oct 3, 2014
@tacaswell
Copy link
Member

This looks reasonable to me. Someone who understand the transform stack better than I do should take a look at this.

@tacaswell tacaswell changed the title Fix problem with legend if data has NaN's Fix problem with legend if data has NaN's [backport to 1.4.x] Oct 3, 2014
@mdboom
Copy link
Member

mdboom commented Oct 3, 2014

Seems like a reasonable fix. It's a shame it has to copy all of the vertices (which can be quite large) to do it, though.

Maybe just use the Numpy error context manager to suppress the warning instead?

@demotu
Copy link
Contributor Author

demotu commented Oct 3, 2014

Yes, using the context manager to suppress the warning will be faster and will use less resources.

We can use:

np.seterr(invalid='ignore')
...

Or:

with np.errstate(invalid='ignore'):
    dx0 = np.sign(vertices[:, 0] - x0)
    dy0 = np.sign(vertices[:, 1] - y0)
    dx1 = np.sign(vertices[:, 0] - x1)
    dy1 = np.sign(vertices[:, 1] - y1)
...

Any preference?

@mdboom
Copy link
Member

mdboom commented Oct 3, 2014

Let's use the context manager. Setting it globally will hide legitimate warnings in user code.

Thanks!

@demotu
Copy link
Contributor Author

demotu commented Oct 3, 2014

Ok, should I change the code and do the pull request?

@tacaswell
Copy link
Member

Just make a new commit and add it to this branch. The PR tracks the branch from your repo, not the commit.

You can even do a force-push to your repo and expunge all record of the current commit.

@demotu demotu force-pushed the master branch 2 times, most recently from 304ddd3 to 76951f0 Compare October 3, 2014 20:46
If data has NaN's and plot has a legend, matplotlib throws a
RuntimeWarning on lines 651-654.
Solution is ignore it.
@efiring
Copy link
Member

efiring commented Oct 5, 2014

The test failure on 2.7 is odd; it is not just the usual unrelated timeout or other Travis glitch.

@tacaswell
Copy link
Member

I have noticed that one go by every so often and it is normally a travis glitch. Restarted the tests on 2.7.

@demotu
Copy link
Contributor Author

demotu commented Oct 5, 2014

it worked, thanks.

efiring added a commit that referenced this pull request Oct 5, 2014
Fix problem with legend if data has NaN's [backport to 1.4.x]
@efiring efiring merged commit d853c82 into matplotlib:master Oct 5, 2014
@efiring
Copy link
Member

efiring commented Oct 5, 2014

@tacaswell, would you do the backport, please?

efiring added a commit that referenced this pull request Oct 5, 2014
Fix problem with legend if data has NaN's [backport to 1.4.x]
@tacaswell
Copy link
Member

back-ported as 86f9f9c

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

Successfully merging this pull request may close these issues.

None yet

5 participants