Skip to content

adding check_xytext for NaN values #30381

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

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

AndrGutierrez
Copy link
Contributor

PR summary

Closes #29516 , I have a few things to address here, this PR fixes errors for this type of case:

import matplotlib.pyplot as plt, numpy as np

ax = plt.figure().add_subplot()
ax.annotate('', (0, 0), (np.nan, np.nan), arrowprops={})

plt.show()

It checks that the coordinates for xytext are a finite number, and if it isn't, the text and the arrow are not drawn, same behaviour as check_xy.

I wasn't able to reproduce the first error in the thread in a similar environment, and for this other piece of code:

import matplotlib.pyplot as plt

ax = plt.figure().add_subplot(projection="polar")
ax.set_ylim(.25, 1.25)
ax.annotate('', (0, .5), (0, 0), arrowprops={})

plt.show()

the error seems to be related to set_ylim, if you remove that, it works. Also, if you change the y coordinate for xy to be less than ylim's bottom, for example:

import matplotlib.pyplot as plt

ax = plt.figure().add_subplot(projection="polar")
ax.set_ylim(.25, 1.25)
ax.annotate('', (0, .2499), (0, 0), arrowprops={})

plt.show()

it seems to work for anything < ylim's bottom (.25 in this case), so I guess the error appears when the text is supposed to be visible, this behaviour is weird and the easiest solution I can think of is just to not apply/allow ylim because it doesn't do anything in polar coordinates, but I don't have enough context to know if that's a good idea or even possible so I would like feedback on that 🙂.

PR checklist

return valid

if all(isinstance(xyt, numbers.Number) for xyt in self.xytext):
valid = not np.isnan(self.xytext).any() and np.isfinite(self.xytext).all()
Copy link
Member

Choose a reason for hiding this comment

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

To fix the polar case, you need to check these values after they are transformed to "screen space", for which you can use self.axes.transData.transform. See the Transforms Tutorial for more details.

I'm not really sure if invalid values here should make the annotation invisible or raise a more helpful error message.

@AndrGutierrez
Copy link
Contributor Author

Ok, I will take a look at it later.

And I agree that not drawing the annotation isn't the best behaviour but it's how it works for check_xy and I wanted to keep it similar, maybe an error message for both would be better

@rcomer
Copy link
Member

rcomer commented Aug 4, 2025

_check_xy only determines what happens if the annotated point lies outside the axes. This could happen frequently if you plot interactively then pan/zoom to show a different part of the plot. The behaviour for this is well-established and documented as a user choice. Here we are checking whether the text point is valid, so I do not think it necessarily needs to be consistent with _check_xy.

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.

[Bug]: Cannot draw arrow from polar plot origin after setting rticks
2 participants
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy