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

Skip to content

Commit 25d259c

Browse files
committed
Correct the default size of text bbox patch and support 'pad' kwarg
1 parent 8eb4e2b commit 25d259c

File tree

1 file changed

+15
-7
lines changed

1 file changed

+15
-7
lines changed

lib/matplotlib/text.py

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ def get_rotation(rotation):
137137
# function as a method with some refactoring of _get_layout method.
138138

139139

140-
def _get_textbox(text, renderer):
140+
def _get_textbox(text, renderer, with_descent=True):
141141
"""
142142
Calculate the bounding box of the text. Unlike
143143
:meth:`matplotlib.text.Text.get_extents` method, The bbox size of
@@ -165,6 +165,10 @@ def _get_textbox(text, renderer):
165165
xt_box, yt_box = min(projected_xs), min(projected_ys)
166166
w_box, h_box = max(projected_xs) - xt_box, max(projected_ys) - yt_box
167167

168+
if not with_descent:
169+
yt_box += d
170+
h_box -= d
171+
168172
tr = mtransforms.Affine2D().rotate(theta)
169173

170174
x_box, y_box = tr.transform_point((xt_box, yt_box))
@@ -232,7 +236,6 @@ def __init__(self,
232236
self._linespacing = linespacing
233237
self.set_rotation_mode(rotation_mode)
234238
self.update(kwargs)
235-
# self.set_bbox(dict(pad=0))
236239

237240
def __getstate__(self):
238241
d = super(Text, self).__getstate__()
@@ -481,10 +484,13 @@ def set_bbox(self, rectprops):
481484

482485
if rectprops is not None:
483486
props = rectprops.copy()
484-
# Dump the pad kwarg; we still need to figure out how to
485-
# use it to expand the box, for backwards compatibility.
486-
pad = props.pop('pad', 4) # noqa
487+
pad = props.pop('pad', 4) # in points; hardwired default
487488
boxstyle = props.pop("boxstyle", "square")
489+
# If pad is in the boxstyle string, it will be passed
490+
# directly to the FancyBboxPatch as font units.
491+
if 'pad' not in boxstyle:
492+
boxstyle += ",pad=%0.2f" % (pad / self.get_size())
493+
488494
bbox_transmuter = props.pop("bbox_transmuter", None)
489495

490496
self._bbox_patch = FancyBboxPatch(
@@ -524,7 +530,8 @@ def update_bbox_position_size(self, renderer):
524530

525531
posx, posy = trans.transform_point((posx, posy))
526532

527-
x_box, y_box, w_box, h_box = _get_textbox(self, renderer)
533+
x_box, y_box, w_box, h_box = _get_textbox(self, renderer,
534+
with_descent=False)
528535
self._bbox_patch.set_bounds(0., 0., w_box, h_box)
529536
theta = np.deg2rad(self.get_rotation())
530537
tr = mtransforms.Affine2D().rotate(theta)
@@ -540,7 +547,8 @@ def _draw_bbox(self, renderer, posx, posy):
540547
(FancyBboxPatch), and draw
541548
"""
542549

543-
x_box, y_box, w_box, h_box = _get_textbox(self, renderer)
550+
x_box, y_box, w_box, h_box = _get_textbox(self, renderer,
551+
with_descent=False)
544552
self._bbox_patch.set_bounds(0., 0., w_box, h_box)
545553
theta = np.deg2rad(self.get_rotation())
546554
tr = mtransforms.Affine2D().rotate(theta)

0 commit comments

Comments
 (0)