Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 8293774

Browse files
authored
Merge pull request #25940 from anntzer/an
Cleanups to Annotation.
2 parents ef56ed5 + ff77c7f commit 8293774

File tree

1 file changed

+25
-33
lines changed

1 file changed

+25
-33
lines changed

lib/matplotlib/text.py

Lines changed: 25 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1478,13 +1478,13 @@ def _get_xy_transform(self, renderer, coords):
14781478
trans = tr + self.axes.transData
14791479
return trans
14801480

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

14851486
bbox0, xy0 = None, None
14861487

1487-
bbox_name, unit = s_
14881488
# if unit is offset-like
14891489
if bbox_name == "figure":
14901490
bbox0 = self.figure.figbbox
@@ -1493,35 +1493,27 @@ def _get_xy_transform(self, renderer, coords):
14931493
elif bbox_name == "axes":
14941494
bbox0 = self.axes.bbox
14951495

1496+
# reference x, y in display coordinate
14961497
if bbox0 is not None:
14971498
xy0 = bbox0.p0
14981499
elif bbox_name == "offset":
14991500
xy0 = self._get_position_xy(renderer)
1500-
1501-
if xy0 is not None:
1502-
# reference x, y in display coordinate
1503-
ref_x, ref_y = xy0
1504-
if unit == "points":
1505-
# dots per points
1506-
dpp = self.figure.dpi / 72
1507-
tr = Affine2D().scale(dpp)
1508-
elif unit == "pixels":
1509-
tr = Affine2D()
1510-
elif unit == "fontsize":
1511-
fontsize = self.get_size()
1512-
dpp = fontsize * self.figure.dpi / 72
1513-
tr = Affine2D().scale(dpp)
1514-
elif unit == "fraction":
1515-
w, h = bbox0.size
1516-
tr = Affine2D().scale(w, h)
1517-
else:
1518-
raise ValueError(f"{unit!r} is not a recognized unit")
1519-
1520-
return tr.translate(ref_x, ref_y)
1521-
15221501
else:
15231502
raise ValueError(f"{coords!r} is not a valid coordinate")
15241503

1504+
if unit == "points":
1505+
tr = Affine2D().scale(self.figure.dpi / 72) # dpi/72 dots per point
1506+
elif unit == "pixels":
1507+
tr = Affine2D()
1508+
elif unit == "fontsize":
1509+
tr = Affine2D().scale(self.get_size() * self.figure.dpi / 72)
1510+
elif unit == "fraction":
1511+
tr = Affine2D().scale(*bbox0.size)
1512+
else:
1513+
raise ValueError(f"{unit!r} is not a recognized unit")
1514+
1515+
return tr.translate(*xy0)
1516+
15251517
def set_annotation_clip(self, b):
15261518
"""
15271519
Set the annotation's clipping behavior.
@@ -1701,15 +1693,15 @@ def transform(renderer) -> Transform
17011693
or callable, default: value of *xycoords*
17021694
The coordinate system that *xytext* is given in.
17031695
1704-
All *xycoords* values are valid as well as the following
1705-
strings:
1696+
All *xycoords* values are valid as well as the following strings:
17061697
1707-
================= =========================================
1698+
================= =================================================
17081699
Value Description
1709-
================= =========================================
1710-
'offset points' Offset (in points) from the *xy* value
1711-
'offset pixels' Offset (in pixels) from the *xy* value
1712-
================= =========================================
1700+
================= =================================================
1701+
'offset points' Offset, in points, from the *xy* value
1702+
'offset pixels' Offset, in pixels, from the *xy* value
1703+
'offset fontsize' Offset, relative to fontsize, from the *xy* value
1704+
================= =================================================
17131705
17141706
arrowprops : dict, optional
17151707
The properties used to draw a `.FancyArrowPatch` arrow between the

0 commit comments

Comments
 (0)