-
-
Notifications
You must be signed in to change notification settings - Fork 8k
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
base: main
Are you sure you want to change the base?
adding check_xytext for NaN values #30381
Conversation
lib/matplotlib/text.py
Outdated
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() |
There was a problem hiding this comment.
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.
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 |
|
Is there a way to run the CircleCI tests before pushing the changes? I always run pytest before pushing, but for circleCI I only see that it fails after it's done |
Yes, you can build the docs locally |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks @AndrGutierrez. This will also need a unit test.
lib/matplotlib/text.py
Outdated
coords = np.array(self.get_transform().transform(self.xytext)) | ||
if all(isinstance(xyt, numbers.Number) for xyt in coords): | ||
valid = not np.isnan(coords).any() and np.isfinite(coords).all() | ||
except TypeError: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why is this in a try-except loop? Did you find examples where a TypeError
is thrown?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, in galleries/examples/units/annotate_with_units.py. got something like this:
TypeError: affine_transform(): incompatible function arguments. The following argument types are supported:
1. (points: typing.Annotated[numpy.typing.ArrayLike, numpy.float64], trans: trans_affine) -> object
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, perhaps you need to use self.get_unitless_position
which applies the axises’ converters so that the result is pure numbers.
Co-authored-by: Ruth Comer <10599679+rcomer@users.noreply.github.com>
Co-authored-by: Ruth Comer <10599679+rcomer@users.noreply.github.com>
I am having an issue with an already existing test:
I made a print for debugging the xytext coordinates used and it shows this:
the original coordinates are (0,0) , but after transformation I got those three, I guess that those nan coordinates are for testing beyond limits, and this new behaviour might be expected, but I'll wait for some guidance on this before moving forward and changing anything in this test. I will create the proper new test for nan values after that. |
It seems to me that that failing The test came in at #25681. I tried reverting the change in --- a/lib/matplotlib/axes/_axes.py
+++ b/lib/matplotlib/axes/_axes.py
@@ -2916,7 +2916,7 @@ class Axes(_AxesBase):
lambda r, b=bar:
mtransforms.Bbox.intersection(
b.get_window_extent(r), b.get_clip_box()
- ) or mtransforms.Bbox.null()
+ )
)
else: # edge
if orientation == "vertical":
diff --git a/lib/matplotlib/text.py b/lib/matplotlib/text.py
index c09490a9ce..800c4c9525 100644
--- a/lib/matplotlib/text.py
+++ b/lib/matplotlib/text.py
@@ -1630,6 +1630,8 @@ class _AnnotationBase:
# check if self.xy is inside the Axes.
xy_pixel = self._get_position_xy(renderer)
return self.axes.contains_point(xy_pixel)
+ if b is None and callable(self.xycoords) and self.xycoords(renderer) is None:
+ return False
return True
This does fix that case, and does not appear to break anything else. However, I am getting out of my depth here so I would appreciate comment from someone else. |
PR summary
Closes #29516 , I have a few things to address here, this PR fixes errors for this type of case:
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:
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:
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