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

Skip to content

Commit 0d0799b

Browse files
authored
Merge pull request #21267 from anntzer/abb
Cleanup AnnotationBbox.
2 parents c660ed7 + 924d7c7 commit 0d0799b

File tree

1 file changed

+27
-49
lines changed

1 file changed

+27
-49
lines changed

lib/matplotlib/offsetbox.py

Lines changed: 27 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1314,7 +1314,9 @@ def __init__(self, offsetbox, xy,
13141314
*textcoords* in `.Annotation` for a detailed description.
13151315
13161316
frameon : bool, default: True
1317-
Whether to draw a frame around the box.
1317+
By default, the text is surrounded by a white `.FancyBboxPatch`
1318+
(accessible as the ``patch`` attribute of the `.AnnotationBbox`).
1319+
If *frameon* is set to False, this patch is made invisible.
13181320
13191321
pad : float, default: 0.4
13201322
Padding around the offsetbox.
@@ -1329,10 +1331,9 @@ def __init__(self, offsetbox, xy,
13291331
"""
13301332

13311333
martist.Artist.__init__(self)
1332-
mtext._AnnotationBase.__init__(self,
1333-
xy,
1334-
xycoords=xycoords,
1335-
annotation_clip=annotation_clip)
1334+
mtext._AnnotationBase.__init__(
1335+
self, xy, xycoords=xycoords, annotation_clip=annotation_clip)
1336+
13361337
self.offsetbox = offsetbox
13371338
self.arrowprops = arrowprops.copy() if arrowprops is not None else None
13381339
self.set_fontsize(fontsize)
@@ -1418,34 +1419,17 @@ def get_fontsize(self):
14181419

14191420
def get_window_extent(self, renderer):
14201421
# docstring inherited
1421-
bboxes = [child.get_window_extent(renderer)
1422-
for child in self.get_children()]
1423-
1424-
return Bbox.union(bboxes)
1422+
return Bbox.union([child.get_window_extent(renderer)
1423+
for child in self.get_children()])
14251424

14261425
def get_tightbbox(self, renderer):
14271426
# docstring inherited
1428-
bboxes = [child.get_tightbbox(renderer)
1429-
for child in self.get_children()]
1430-
1431-
return Bbox.union(bboxes)
1427+
return Bbox.union([child.get_tightbbox(renderer)
1428+
for child in self.get_children()])
14321429

14331430
def update_positions(self, renderer):
14341431
"""
1435-
Update the pixel positions of the annotated point and the text.
1436-
"""
1437-
xy_pixel = self._get_position_xy(renderer)
1438-
self._update_position_xybox(renderer, xy_pixel)
1439-
1440-
mutation_scale = renderer.points_to_pixels(self.get_fontsize())
1441-
self.patch.set_mutation_scale(mutation_scale)
1442-
1443-
if self.arrow_patch:
1444-
self.arrow_patch.set_mutation_scale(mutation_scale)
1445-
1446-
def _update_position_xybox(self, renderer, xy_pixel):
1447-
"""
1448-
Update the pixel positions of the annotation text and the arrow patch.
1432+
Update pixel positions for the annotated point, the text and the arrow.
14491433
"""
14501434

14511435
x, y = self.xybox
@@ -1458,40 +1442,34 @@ def _update_position_xybox(self, renderer, xy_pixel):
14581442
ox0, oy0 = self._get_xy(renderer, x, y, self.boxcoords)
14591443

14601444
w, h, xd, yd = self.offsetbox.get_extent(renderer)
1445+
fw, fh = self._box_alignment
1446+
self.offsetbox.set_offset((ox0 - fw * w + xd, oy0 - fh * h + yd))
14611447

1462-
_fw, _fh = self._box_alignment
1463-
self.offsetbox.set_offset((ox0 - _fw * w + xd, oy0 - _fh * h + yd))
1464-
1465-
# update patch position
14661448
bbox = self.offsetbox.get_window_extent(renderer)
14671449
self.patch.set_bounds(bbox.bounds)
14681450

1469-
ox1, oy1 = xy_pixel
1451+
mutation_scale = renderer.points_to_pixels(self.get_fontsize())
1452+
self.patch.set_mutation_scale(mutation_scale)
14701453

14711454
if self.arrowprops:
1472-
d = self.arrowprops.copy()
1473-
14741455
# Use FancyArrowPatch if self.arrowprops has "arrowstyle" key.
14751456

14761457
# Adjust the starting point of the arrow relative to the textbox.
14771458
# TODO: Rotation needs to be accounted.
1478-
relpos = self._arrow_relpos
1479-
1480-
ox0 = bbox.x0 + bbox.width * relpos[0]
1481-
oy0 = bbox.y0 + bbox.height * relpos[1]
1482-
1483-
# The arrow will be drawn from (ox0, oy0) to (ox1, oy1).
1484-
# It will be first clipped by patchA and patchB.
1485-
# Then it will be shrunk by shrinkA and shrinkB (in points).
1486-
# If patch A is not set, self.bbox_patch is used.
1487-
1488-
self.arrow_patch.set_positions((ox0, oy0), (ox1, oy1))
1489-
fs = self.prop.get_size_in_points()
1490-
mutation_scale = d.pop("mutation_scale", fs)
1491-
mutation_scale = renderer.points_to_pixels(mutation_scale)
1459+
arrow_begin = bbox.p0 + bbox.size * self._arrow_relpos
1460+
arrow_end = self._get_position_xy(renderer)
1461+
# The arrow (from arrow_begin to arrow_end) will be first clipped
1462+
# by patchA and patchB, then shrunk by shrinkA and shrinkB (in
1463+
# points). If patch A is not set, self.bbox_patch is used.
1464+
self.arrow_patch.set_positions(arrow_begin, arrow_end)
1465+
1466+
if "mutation_scale" in self.arrowprops:
1467+
mutation_scale = renderer.points_to_pixels(
1468+
self.arrowprops["mutation_scale"])
1469+
# Else, use fontsize-based mutation_scale defined above.
14921470
self.arrow_patch.set_mutation_scale(mutation_scale)
14931471

1494-
patchA = d.pop("patchA", self.patch)
1472+
patchA = self.arrowprops.get("patchA", self.patch)
14951473
self.arrow_patch.set_patchA(patchA)
14961474

14971475
def draw(self, renderer):

0 commit comments

Comments
 (0)