Skip to content

Cleanups to Annotation. #25940

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

Merged
merged 1 commit into from
May 23, 2023
Merged
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
58 changes: 25 additions & 33 deletions lib/matplotlib/text.py
Original file line number Diff line number Diff line change
Expand Up @@ -1478,13 +1478,13 @@ def _get_xy_transform(self, renderer, coords):
trans = tr + self.axes.transData
return trans

s_ = coords.split()
if len(s_) != 2:
raise ValueError(f"{coords!r} is not a valid coordinate")
try:
bbox_name, unit = coords.split()
except ValueError: # i.e. len(coords.split()) != 2.
raise ValueError(f"{coords!r} is not a valid coordinate") from None

bbox0, xy0 = None, None

bbox_name, unit = s_
# if unit is offset-like
if bbox_name == "figure":
bbox0 = self.figure.figbbox
Expand All @@ -1493,35 +1493,27 @@ def _get_xy_transform(self, renderer, coords):
elif bbox_name == "axes":
bbox0 = self.axes.bbox

# reference x, y in display coordinate
if bbox0 is not None:
xy0 = bbox0.p0
elif bbox_name == "offset":
xy0 = self._get_position_xy(renderer)

if xy0 is not None:
# reference x, y in display coordinate
ref_x, ref_y = xy0
if unit == "points":
# dots per points
dpp = self.figure.dpi / 72
tr = Affine2D().scale(dpp)
elif unit == "pixels":
tr = Affine2D()
elif unit == "fontsize":
fontsize = self.get_size()
dpp = fontsize * self.figure.dpi / 72
tr = Affine2D().scale(dpp)
elif unit == "fraction":
w, h = bbox0.size
tr = Affine2D().scale(w, h)
else:
raise ValueError(f"{unit!r} is not a recognized unit")

return tr.translate(ref_x, ref_y)

else:
raise ValueError(f"{coords!r} is not a valid coordinate")

if unit == "points":
tr = Affine2D().scale(self.figure.dpi / 72) # dpi/72 dots per point
elif unit == "pixels":
tr = Affine2D()
elif unit == "fontsize":
tr = Affine2D().scale(self.get_size() * self.figure.dpi / 72)
elif unit == "fraction":
tr = Affine2D().scale(*bbox0.size)
else:
raise ValueError(f"{unit!r} is not a recognized unit")

return tr.translate(*xy0)

def set_annotation_clip(self, b):
"""
Set the annotation's clipping behavior.
Expand Down Expand Up @@ -1701,15 +1693,15 @@ def transform(renderer) -> Transform
or callable, default: value of *xycoords*
The coordinate system that *xytext* is given in.

All *xycoords* values are valid as well as the following
strings:
All *xycoords* values are valid as well as the following strings:

================= =========================================
================= =================================================
Value Description
================= =========================================
'offset points' Offset (in points) from the *xy* value
'offset pixels' Offset (in pixels) from the *xy* value
================= =========================================
================= =================================================
'offset points' Offset, in points, from the *xy* value
'offset pixels' Offset, in pixels, from the *xy* value
'offset fontsize' Offset, relative to fontsize, from the *xy* value
================= =================================================

arrowprops : dict, optional
The properties used to draw a `.FancyArrowPatch` arrow between the
Expand Down