8000 re factored `_AnnoationBase` to push keeping track of there the · adrn/matplotlib@3363b12 · GitHub
[go: up one dir, main page]

Skip to content

Commit 3363b12

Browse files
committed
re factored _AnnoationBase to push keeping track of there the
secondary object is to the sub-classes. Added properties to maintain back-compatibility added properties to replace xytext and textcoords (xyann, anncoords) properly deprecated `textcoord` and `xytext` in favor of `anncoords` and `xyann`.
1 parent a65151b commit 3363b12

File tree

4 files changed

+108
-41
lines changed

4 files changed

+108
-41
lines changed

doc/api/api_changes.rst

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,24 @@ original location:
5959
thus `colorbar.ColorbarBase.outline` is now a
6060
`matplotlib.patches.Polygon` object.
6161

62+
6263
* The rcParams `savefig.transparent` has been added to control
6364
default transparency when saving figures.
6465

66+
* Slightly refactored the `Annotation` family. The text location in
67+
`Annotation` is now handled entirely handled by the underlying `Text`
68+
object so `set_position` works as expected. The attributes `xytext` and
69+
`textcoords` have been deprecated in favor of `xyann` and `anncoords` so
70+
that `Annotation` and `AnnotaionBbox` can share a common sensibly named
71+
api for getting/setting the location of the text or box.
72+
73+
- `xyann` -> set the location of the annotation
74+
- `xy` -> set where the arrow points to
75+
- `anncoords` -> set the units of the annotation location
76+
- `xycoords` -> set the units of the point location
77+
- `set_position()` -> `Annotation` only set location of annotation
78+
79+
6580
.. _changes_in_1_3:
6681

6782

lib/matplotlib/offsetbox.py

Lines changed: 36 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1265,6 +1265,10 @@ def __init__(self, offsetbox, xy,
12651265

12661266
self.set_fontsize(fontsize)
12671267

1268+
self.xybox = xybox
1269+
1270+
self.boxcoords = boxcoords
1271+
12681272
if arrowprops is not None:
12691273
self._arrow_relpos = self.arrowprops.pop("relpos", (0.5, 0.5))
12701274
self.arrow_patch = FancyArrowPatch((0, 0), (1, 1),
@@ -1274,8 +1278,8 @@ def __init__(self, offsetbox, xy,
12741278
self.arrow_patch = None
12751279

12761280
_AnnotationBase.__init__(self,
1277-
xy, xytext=xybox,
1278-
xycoords=xycoords, textcoords=boxcoords,
1281+
xy,
1282+
xycoords=xycoords,
12791283
annotation_clip=annotation_clip)
12801284

12811285
martist.Artist.__init__(self, **kwargs)
@@ -1295,6 +1299,22 @@ def __init__(self, offsetbox, xy,
12951299
self.patch.set(**bboxprops)
12961300
self._drawFrame = frameon
12971301

1302+
@property
1303+
def xyann(self):
1304+
return self.xybox
1305+
1306+
@xyann.setter
1307+
def xyann(self, xyann):
1308+
self.xybox = xyann
1309+
1310+
@property
1311+
def anncoords(self):
1312+
return self.boxcoords
1313+
1314+
@anncoords.setter
1315+
def anncoords(self, coords):
1316+
self.boxcoords = coords
1317+
12981318
def contains(self, event):
12991319
t, tinfo = self.offsetbox.contains(event)
13001320
#if self.arrow_patch is not None:
@@ -1352,14 +1372,14 @@ def _update_position_xybox(self, renderer, xy_pixel):
13521372
patch.
13531373
"""
13541374

1355-
x, y = self.xytext
1356-
if isinstance(self.textcoords, tuple):
1357-
xcoord, ycoord = self.textcoords
1375+
x, y = self.xybox
1376+
if isinstance(self.boxcoords, tuple):
1377+
xcoord, ycoord = self.boxcoords
13581378
x1, y1 = self._get_xy(renderer, x, y, xcoord)
13591379
x2, y2 = self._get_xy(renderer, x, y, ycoord)
13601380
ox0, oy0 = x1, y2
13611381
else:
1362-
ox0, oy0 = self._get_xy(renderer, x, y, self.textcoords)
1382+
ox0, oy0 = self._get_xy(renderer, x, y, self.boxcoords)
13631383

13641384
w, h, xd, yd = self.offsetbox.get_extent(renderer)
13651385

@@ -1579,32 +1599,30 @@ def __init__(self, annotation, use_blit=False):
15791599

15801600
def save_offset(self):
15811601
ann = self.annotation
1582-
x, y = ann.xytext
1583-
if isinstance(ann.textcoords, tuple):
1584-
xcoord, ycoord = ann.textcoords
1602+
x, y = ann.xyann
1603+
if isinstance(ann.anncoords, tuple):
1604+
xcoord, ycoord = ann.anncoords
15851605
x1, y1 = ann._get_xy(self.canvas.renderer, x, y, xcoord)
15861606
x2, y2 = ann._get_xy(self.canvas.renderer, x, y, ycoord)
15871607
ox0, oy0 = x1, y2
15881608
else:
1589-
ox0, oy0 = ann._get_xy(self.canvas.renderer, x, y, ann.textcoords)
1609+
ox0, oy0 = ann._get_xy(self.canvas.renderer, x, y, ann.anncoords)
15901610

15911611
self.ox, self.oy = ox0, oy0
1592-
self.annotation.textcoords = "figure pixels"
1612+
self.annotation.anncoords = "figure pixels"
15931613
self.update_offset(0, 0)
15941614

15951615
def update_offset(self, dx, dy):
15961616
ann = self.annotation
1597-
ann.xytext = self.ox + dx, self.oy + dy
1598-
x, y = ann.xytext
1599-
# xy is never used
1600-
xy = ann._get_xy(self.canvas.renderer, x, y, ann.textcoords)
1617+
ann.xyann = self.ox + dx, self.oy + dy
1618+
x, y = ann.xyann
16011619

16021620
def finalize_offset(self):
1603-
loc_in_canvas = self.annotation.xytext
1604-
self.annotation.textcoords = "axes fraction"
1621+
loc_in_canvas = self.annotation.xyann
1622+
self.annotation.anncoords = "axes fraction"
16051623
pos_axes_fraction = self.annotation.axes.transAxes.inverted()
16061624
pos_axes_fraction = pos_axes_fraction.transform_point(loc_in_canvas)
1607-
self.annotation.xytext = tuple(pos_axes_fraction)
1625+
self.annotation.xyann = tuple(pos_axes_fraction)
16081626

16091627

16101628
if __name__ == "__main__":

lib/matplotlib/text.py

Lines changed: 56 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1435,19 +1435,12 @@ def __call__(self, renderer):
14351435

14361436
class _AnnotationBase(object):
14371437
def __init__(self,
1438-
xy, xytext=None,
1439-
xycoords='data', textcoords=None,
1438+
xy,
1439+
xycoords='data',
14401440
annotation_clip=None):
1441-
if xytext is None:
1442-
xytext = xy
1443-
if textcoords is None:
1444-
textcoords = xycoords
1445-
# we'll draw ourself after the artist we annotate by default
1446-
x, y = self.xytext = xytext
14471441

14481442
self.xy = xy
14491443
self.xycoords = xycoords
1450-
self.textcoords = textcoords
14511444
self.set_annotation_clip(annotation_clip)
14521445

14531446
self._draggable = None
@@ -1655,6 +1648,26 @@ def draggable(self, state=None, use_blit=False):
16551648

16561649
return self._draggable
16571650

1651+
@property
1652+
@cbook.deprecated('1.4', message='Use `anncoords` instead', name='textcoords', alternative='anncoords')
1653+
def textcoords(self):
1654+
return self.anncoords
1655+
1656+
@textcoords.setter
1657+
@cbook.deprecated('1.4', message='Use `anncoords` instead', name='textcoords', alternative='anncoords')
1658+
def textcoords(self, val):
1659+
self.anncoords = val
1660+
1661+
@property
1662+
@cbook.deprecated('1.4', message='Use `xyann` instead', name='xytext', alternative='xyann')
1663+
def xytext(self):
1664+
self.xyann
1665+
1666+
@xytext.setter
1667+
@cbook.deprecated('1.4', message='Use `xyann` instead', name='xytext', alternative='xyann')
1668+
def xytext(self, val):
1669+
self.xyann = val
1670+
16581671

16591672
class Annotation(Text, _AnnotationBase):
16601673
"""
@@ -1778,11 +1791,20 @@ def __init__(self, s, xy,
17781791
"""
17791792

17801793
_AnnotationBase.__init__(self,
1781-
xy, xytext=xytext,
1782-
xycoords=xycoords, textcoords=textcoords,
1794+
xy,
1795+
xycoords=xycoords,
17831796
annotation_clip=annotation_clip)
17841797

1785-
x, y = self.xytext
1798+
# clean up textcoords and assign default
1799+
if textcoords is None:
1800+
textcoords = self.xycoords
1801+
self._textcoords = textcoords
1802+
1803+
# cleanup xytext defaults
1804+
if xytext is None:
1805+
xytext = self.xy
1806+
x, y = xytext
1807+
17861808
Text.__init__(self, x, y, s, **kwargs)
17871809

17881810
self.arrowprops = arrowprops
@@ -1802,10 +1824,26 @@ def contains(self, event):
18021824
if self.arrow is not None:
18031825
in_arrow, _ = self.arrow.contains(event)
18041826
contains = contains or in_arrow
1805-
# self.arrow_patch is currently not checked as this can be a line - JJ
1827+
# self.arrow_patch is currently not checked as this can be a line - J
18061828

18071829
return contains, tinfo
18081830

1831+
@property
1832+
def xyann(self):
1833+
return self.get_position()
1834+
1835+
@xyann.setter
1836+
def xyann(self, xytext):
1837+
self.set_position(xytext)
1838+
1839+
@property
1840+
def anncoords(self):
1841+
return self._textcoords
1842+
1843+
@anncoords.setter
1844+
def anncoords(self, coords):
1845+
self._textcoords = coords
1846+
18091847
def set_figure(self, fig):
18101848

18111849
if self.arrow is not None:
@@ -1825,18 +1863,14 @@ def _update_position_xytext(self, renderer, xy_pixel):
18251863
"""Update the pixel positions of the annotation text and the arrow
18261864
patch.
18271865
"""
1866+
# generate transformation,
1867+
self.set_transform(self._get_xy_transform(renderer, self.anncoords))
18281868

1829-
x, y = self.xytext
1830-
self._x, self._y = self._get_xy(renderer, x, y,
1831-
self.textcoords)
1832-
1833-
x, y = xy_pixel
1834-
1835-
ox0, oy0 = self._x, self._y
1836-
ox1, oy1 = x, y
1869+
ox0, oy0 = self._get_xy_display()
1870+
ox1, oy1 = xy_pixel
18371871

18381872
if self.arrowprops:
1839-
x0, y0 = x, y
1873+
x0, y0 = xy_pixel
18401874
l, b, w, h = self.get_window_extent(renderer).bounds
18411875
r = l + w
18421876
t = b + h

lib/mpl_toolkits/axisartist/axis_artist.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1378,7 +1378,7 @@ def _update_offsetText(self):
13781378
self.offsetText.set_text( self.axis.major.formatter.get_offset() )
13791379
self.offsetText.set_size(self.major_ticklabels.get_size())
13801380
offset = self.major_ticklabels.get_pad() + self.major_ticklabels.get_size() + 2.
1381-
self.offsetText.xytext= (0, offset)
1381+
self.offsetText.xyann= (0, offset)
13821382

13831383

13841384
def _draw_offsetText(self, renderer):

0 commit comments

Comments
 (0)
0