-
-
Notifications
You must be signed in to change notification settings - Fork 7.9k
Fix Annotation.contains. #12164
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 Annotation.contains. #12164
Conversation
Annotation.contains always calls get_window_extents() without setting the renderer arg, which is thus None (and this is implicitly allowed by the signature of get_window_extents), but get_window_extents calls update_positions which *requires* a non-None renderer (it ultimately calls renderer.points_to_pixels). Instead, reuse the code from Text.get_window_extents() which defaults to the artist-or-figure renderer if the arg is None. Example breaking code: from matplotlib import pyplot as plt ann = plt.annotate( "foo", (.5, .5), arrowprops={"arrowstyle": "->"}, textcoords="offset points", xytext=(10, 10)) plt.gcf().canvas.mpl_connect( "button_press_event", lambda event: print(ann.contains(event))) plt.show() and click anywhere to trigger an AttributeError.
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.
Modulo making these errors more verbose?
if self._renderer is None: | ||
self._renderer = self.figure._cachedRenderer | ||
if self._renderer is None: | ||
raise RuntimeError('Cannot get window extent w/o renderer') |
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.
Should we be advising users to trigger a draw in these error messages?
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.
There's a few other places which already have a similar warning (Axes.draw_artist, Axes.redraw_in_frame) so I'd rather just factor it out to a helper method in a separate PR.
Something went wrong ... Please have a look at my logs. |
@meeseeksdev backport to v3.0.x |
Annotation.contains always calls get_window_extents() without setting
the renderer arg, which is thus None (and this is implicitly allowed by
the signature of get_window_extents), but get_window_extents calls
update_positions which requires a non-None renderer (it ultimately
calls renderer.points_to_pixels).
Instead, reuse the code from Text.get_window_extents() which defaults to
the artist-or-figure renderer if the arg is None.
Example breaking code:
and click anywhere to trigger an AttributeError.
Regression due to #11801.
PR Summary
PR Checklist