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

Skip to content

Commit c4cdc8c

Browse files
committed
Refactor Annotation properties.
- Make xycoords a property to validate inputs at set-time, rather than at draw time. - First define get_anncoords and set_anncoords, and then the anncoords property from them (the danse is just so that anncoords and get_anncoords get different docstrings...) as that order avoids having to fiddle with fget and fset.
1 parent 7ff965a commit c4cdc8c

File tree

1 file changed

+30
-31
lines changed

1 file changed

+30
-31
lines changed

lib/matplotlib/text.py

Lines changed: 30 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1456,19 +1456,10 @@ def _get_xy_transform(self, renderer, s):
14561456

14571457
def _get_ref_xy(self, renderer):
14581458
"""
1459-
return x, y (in display coordinate) that is to be used for a reference
1460-
of any offset coordinate
1459+
Return x, y (in display coordinates) that is to be used for a reference
1460+
of any offset coordinate.
14611461
"""
1462-
def is_offset(s):
1463-
return isinstance(s, str) and s.split()[0] == "offset"
1464-
1465-
if isinstance(self.xycoords, tuple):
1466-
if any(map(is_offset, self.xycoords)):
1467-
raise ValueError("xycoords should not be an offset coordinate")
1468-
elif is_offset(self.xycoords):
1469-
raise ValueError("xycoords should not be an offset coordinate")
1470-
x, y = self.xy
1471-
return self._get_xy(renderer, x, y, self.xycoords)
1462+
return self._get_xy(renderer, *self.xy, self.xycoords)
14721463

14731464
# def _get_bbox(self, renderer):
14741465
# if hasattr(bbox, "bounds"):
@@ -1796,10 +1787,22 @@ def contains(self, event):
17961787
contains = contains or in_patch
17971788
return contains, tinfo
17981789

1790+
@property
1791+
def xycoords(self):
1792+
return self._xycoords
1793+
1794+
@xycoords.setter
1795+
def xycoords(self, xycoords):
1796+
def is_offset(s): return isinstance(s, str) and s.startswith("offset")
1797+
if (isinstance(xycoords, tuple) and any(map(is_offset, xycoords))
1798+
or is_offset(xycoords)):
1799+
raise ValueError("xycoords should not be an offset coordinate")
1800+
self._xycoords = xycoords
1801+
17991802
@property
18001803
def xyann(self):
18011804
"""
1802-
The the text position.
1805+
The text position.
18031806
18041807
See also *xytext* in `.Annotation`.
18051808
"""
@@ -1809,28 +1812,24 @@ def xyann(self):
18091812
def xyann(self, xytext):
18101813
self.set_position(xytext)
18111814

1812-
@property
1813-
def anncoords(self):
1814-
"""The coordinate system to use for `.Annotation.xyann`."""
1815-
return self._textcoords
1816-
1817-
@anncoords.setter
1818-
def anncoords(self, coords):
1819-
self._textcoords = coords
1815+
def get_anncoords(self):
1816+
"""
1817+
Return the coordinate system to use for `.Annotation.xyann`.
18201818
1821-
get_anncoords = anncoords.fget
1822-
get_anncoords.__doc__ = """
1823-
Return the coordinate system to use for `.Annotation.xyann`.
1819+
See also *xycoords* in `.Annotation`.
1820+
"""
1821+
return self._textcoords
18241822

1825-
See also *xycoords* in `.Annotation`.
1826-
"""
1823+
def set_anncoords(self, coords):
1824+
"""
1825+
Set the coordinate system to use for `.Annotation.xyann`.
18271826
1828-
set_anncoords = anncoords.fset
1829-
set_anncoords.__doc__ = """
1830-
Set the coordinate system to use for `.Annotation.xyann`.
1827+
See also *xycoords* in `.Annotation`.
1828+
"""
1829+
self._textcoords = coords
18311830

1832-
See also *xycoords* in `.Annotation`.
1833-
"""
1831+
anncoords = property(get_anncoords, set_anncoords, doc="""
1832+
The coordinate system to use for `.Annotation.xyann`.""")
18341833

18351834
def set_figure(self, fig):
18361835
if self.arrow_patch is not None:

0 commit comments

Comments
 (0)