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

Skip to content

Commit f81b068

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 dance is just so that anncoords and get_anncoords get different docstrings...) as that order avoids having to fiddle with fget and fset.
1 parent 8451629 commit f81b068

File tree

1 file changed

+31
-30
lines changed

1 file changed

+31
-30
lines changed

lib/matplotlib/text.py

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

14611461
def _get_ref_xy(self, renderer):
14621462
"""
1463-
Return x, y (in display coordinate) that is to be used for a reference
1463+
Return x, y (in display coordinates) that is to be used for a reference
14641464
of any offset coordinate.
14651465
"""
1466-
def is_offset(s):
1467-
return isinstance(s, str) and s.split()[0] == "offset"
1468-
1469-
if isinstance(self.xycoords, tuple):
1470-
if any(map(is_offset, self.xycoords)):
1471-
raise ValueError("xycoords should not be an offset coordinate")
1472-
elif is_offset(self.xycoords):
1473-
raise ValueError("xycoords should not be an offset coordinate")
1474-
x, y = self.xy
1475-
return self._get_xy(renderer, x, y, self.xycoords)
1466+
return self._get_xy(renderer, *self.xy, self.xycoords)
14761467

14771468
# def _get_bbox(self, renderer):
14781469
# if hasattr(bbox, "bounds"):
@@ -1795,10 +1786,24 @@ def contains(self, event):
17951786
contains = contains or in_patch
17961787
return contains, tinfo
17971788

1789+
@property
1790+
def xycoords(self):
1791+
return self._xycoords
1792+
1793+
@xycoords.setter
1794+
def xycoords(self, xycoords):
1795+
def is_offset(s):
1796+
return isinstance(s, str) and s.startswith("offset")
1797+
1798+
if (isinstance(xycoords, tuple) and any(map(is_offset, xycoords))
1799+
or is_offset(xycoords)):
1800+
raise ValueError("xycoords cannot be an offset coordinate")
1801+
self._xycoords = xycoords
1802+
17981803
@property
17991804
def xyann(self):
18001805
"""
1801-
The the text position.
1806+
The text position.
18021807
18031808
See also *xytext* in `.Annotation`.
18041809
"""
@@ -1808,28 +1813,24 @@ def xyann(self):
18081813
def xyann(self, xytext):
18091814
self.set_position(xytext)
18101815

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

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

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

18341835
def set_figure(self, fig):
18351836
# docstring inherited

0 commit comments

Comments
 (0)