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

Skip to content

Commit bf4bae6

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

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
@@ -118,7 +118,7 @@ def get_rotation(rotation):
118118
# function as a method with some refactoring of _get_layout method.
119119

120120

121-
def _get_textbox(text, renderer):
121+
def _get_textbox(text, renderer, with_descent=True):
122122
"""
123123
Calculate the bounding box of the text. Unlike
124124
:meth:`matplotlib.text.Text.get_extents` method, The bbox size of
@@ -146,6 +146,10 @@ def _get_textbox(text, renderer):
146146
xt_box, yt_box = min(projected_xs), min(projected_ys)
147147
w_box, h_box = max(projected_xs) - xt_box, max(projected_ys) - yt_box
148148

149+
if not with_descent:
150+
yt_box += d
151+
h_box -= d
152+
149153
tr = mtransforms.Affine2D().rotate(theta)
150154

151155
x_box, y_box = tr.transform_point((xt_box, yt_box))
@@ -211,7 +215,6 @@ def __init__(self,
211215
self._linespacing = linespacing
212216
self.set_rotation_mode(rotation_mode)
213217
self.update(kwargs)
214-
#self.set_bbox(dict(pad=0))
215218

216219
def __getstate__(self):
217220
d = super(Text, self).__getstate__()
@@ -458,10 +461,13 @@ def set_bbox(self, rectprops):
458461

459462
if rectprops is not None:
460463
props = rectprops.copy()
461-
# Dump the pad kwarg; we still need to figure out how to
462-
# use it to expand the box, for backwards compatibility.
463-
pad = props.pop('pad', 4) # noqa
464+
pad = props.pop('pad', 4) # in points; hardwired default
464465
boxstyle = props.pop("boxstyle", "square")
466+
# If pad is in the boxstyle string, it will be passed
467+
# directly to the FancyBboxPatch as font units.
468+
if 'pad' not in boxstyle:
469+
boxstyle += ",pad=%0.2f" % (pad / self.get_size())
470+
465471
bbox_transmuter = props.pop("bbox_transmuter", None)
466472

467473
self._bbox_patch = FancyBboxPatch(
@@ -501,7 +507,8 @@ def update_bbox_position_size(self, renderer):
501507

502508
posx, posy = trans.transform_point((posx, posy))
503509

504-
x_box, y_box, w_box, h_box = _get_textbox(self, renderer)
510+
x_box, y_box, w_box, h_box = _get_textbox(self, renderer,
511+
with_descent=False)
505512
self._bbox_patch.set_bounds(0., 0., w_box, h_box)
506513
theta = np.deg2rad(self.get_rotation())
507514
tr = mtransforms.Affine2D().rotate(theta)
@@ -517,7 +524,8 @@ def _draw_bbox(self, renderer, posx, posy):
517524
(FancyBboxPatch), and draw
518525
"""
519526

520-
x_box, y_box, w_box, h_box = _get_textbox(self, renderer)
527+
x_box, y_box, w_box, h_box = _get_textbox(self, renderer,
528+
with_descent=False)
521529
self._bbox_patch.set_bounds(0., 0., w_box, h_box)
522530
theta = np.deg2rad(self.get_rotation())
523531
tr = mtransforms.Affine2D().rotate(theta)

0 commit comments

Comments
 (0)