adding check_xytext for NaN values by AndrGutierrez · Pull Request #30381 · matplotlib/matplotlib · GitHub
Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
3 changes: 2 additions & 1 deletion lib/matplotlib/backends/web_backend/.eslintrc.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

43 changes: 41 additions & 2 deletions lib/matplotlib/text.py
Original file line number Diff line number Diff line change
Expand Up @@ -1625,12 +1625,41 @@ def _check_xy(self, renderer=None):
if renderer is None:
renderer = self.get_figure(root=True)._get_renderer()
b = self.get_annotation_clip()
not_none = (b is None and
callable(self.xycoords) and
self.xycoords(renderer) is not None)
if b or (b is None and self.xycoords == "data"):
# check if self.xy is inside the Axes.
xy_pixel = self._get_position_xy(renderer)
return self.axes.contains_point(xy_pixel)

if not_none:
if self.xycoords(renderer).is_null():
return False

return True

def _check_xytext(self, renderer=None):
"""Check whether the annotation text at *xy_pixel* can be drawn."""
valid = True

if self._xytext is None:
return valid

# transforming the coordinates
x = self.convert_xunits(self._xytext[0])
y = self.convert_yunits(self._xytext[1])
unitless_coords = (x, y)
coords = np.array(self.get_transform().transform(unitless_coords))
valid = not np.isnan(coords).any() and np.isfinite(coords).all()
# DEBUG
print("###")
print(coords)

if not valid:
raise ValueError("xytext coordinates must be finite numbers")
return valid

def draggable(self, state=None, use_blit=False):
"""
Set whether the annotation is draggable with the mouse.
Expand Down Expand Up @@ -1862,6 +1891,7 @@ def transform(renderer) -> Transform
xy,
xycoords=xycoords,
annotation_clip=annotation_clip)
self._xytext = xytext
# warn about wonky input data
if (xytext is None and
textcoords is not None and
Expand Down Expand Up @@ -2027,11 +2057,16 @@ def draw(self, renderer):
# docstring inherited
if renderer is not None:
self._renderer = renderer
if not self.get_visible() or not self._check_xy(renderer):

visible = self.get_visible() and self._check_xy(renderer)


if not visible:
return
# Update text positions before `Text.draw` would, so that the
# FancyArrowPatch is correctly positioned.
self.update_positions(renderer)
self._check_xytext()
self.update_bbox_position_size(renderer)
if self.arrow_patch is not None: # FancyArrowPatch
if (self.arrow_patch.get_figure(root=False) is None and
Expand All @@ -2046,7 +2081,10 @@ def get_window_extent(self, renderer=None):
# docstring inherited
# This block is the same as in Text.get_window_extent, but we need to
# set the renderer before calling update_positions().
if not self.get_visible() or not self._check_xy(renderer):
visible = self.get_visible() and self._check_xy(renderer)


if not visible:
return Bbox.unit()
if renderer is not None:
self._renderer = renderer
Expand All @@ -2056,6 +2094,7 @@ def get_window_extent(self, renderer=None):
raise RuntimeError('Cannot get window extent without renderer')

self.update_positions(self._renderer)
self._check_xytext()

text_bbox = Text.get_window_extent(self)
bboxes = [text_bbox]
Expand Down
8 changes: 8 additions & 0 deletions lib/matplotlib/transforms.py
Loading