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

Skip to content

Commit 2eb4313

Browse files
committed
handle pad defaults; fix sizing of YAArrow substitute
1 parent 1e6af77 commit 2eb4313

File tree

2 files changed

+36
-24
lines changed

2 files changed

+36
-24
lines changed

lib/matplotlib/tests/test_text.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -400,12 +400,14 @@ def test_text_with_arrow_annotation_get_window_extent():
400400

401401
@cleanup
402402
def test_arrow_annotation_get_window_extent():
403-
figure = Figure(dpi=100)
403+
dpi = 100
404+
dots_per_point = dpi / 72
405+
figure = Figure(dpi=dpi)
404406
figure.set_figwidth(2.0)
405407
figure.set_figheight(2.0)
406408
renderer = RendererAgg(200, 200, 100)
407409

408-
# Text annotation with arrow
410+
# Text annotation with arrow; arrow dimensions are in points
409411
annotation = Annotation(
410412
'', xy=(0.0, 50.0), xytext=(50.0, 50.0), xycoords='figure pixels',
411413
arrowprops={
@@ -417,9 +419,9 @@ def test_arrow_annotation_get_window_extent():
417419
points = bbox.get_points()
418420

419421
eq_(bbox.width, 50.0)
420-
assert_almost_equal(bbox.height, 10.0 / 0.72)
422+
assert_almost_equal(bbox.height, 10.0 * dots_per_point)
421423
eq_(points[0, 0], 0.0)
422-
eq_(points[0, 1], 50.0 - 5 / 0.72)
424+
eq_(points[0, 1], 50.0 - 5 * dots_per_point)
423425

424426

425427
@cleanup

lib/matplotlib/text.py

Lines changed: 30 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,8 @@ def get_rotation(rotation):
9393
animated [True | False]
9494
backgroundcolor any matplotlib color
9595
bbox rectangle prop dict plus key 'pad' which is a
96-
pad in points
96+
pad in points; if a boxstyle is supplied, then
97+
pad is instead a fraction of the font size
9798
clip_box a matplotlib.transform.Bbox instance
9899
clip_on [True | False]
99100
color any matplotlib color
@@ -137,7 +138,7 @@ def get_rotation(rotation):
137138
# function as a method with some refactoring of _get_layout method.
138139

139140

140-
def _get_textbox(text, renderer, with_descent=True):
141+
def _get_textbox(text, renderer):
141142
"""
142143
Calculate the bounding box of the text. Unlike
143144
:meth:`matplotlib.text.Text.get_extents` method, The bbox size of
@@ -165,10 +166,6 @@ def _get_textbox(text, renderer, with_descent=True):
165166
xt_box, yt_box = min(projected_xs), min(projected_ys)
166167
w_box, h_box = max(projected_xs) - xt_box, max(projected_ys) - yt_box
167168

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

174171
x_box, y_box = tr.transform_point((xt_box, yt_box))
@@ -228,7 +225,6 @@ def __init__(self,
228225
self._multialignment = multialignment
229226
self._rotation = rotation
230227
self._fontproperties = fontproperties
231-
self._bbox = None
232228
self._bbox_patch = None # a FancyBboxPatch instance
233229
self._renderer = None
234230
if linespacing is None:
@@ -237,6 +233,14 @@ def __init__(self,
237233
self.set_rotation_mode(rotation_mode)
238234
self.update(kwargs)
239235

236+
def update(self, kwargs):
237+
"""
238+
Update properties from a dictionary.
239+
"""
240+
bbox = kwargs.pop('bbox', None)
241+
super(Text, self).update(kwargs)
242+
self.set_bbox(bbox) # depends on font properties
243+
240244
def __getstate__(self):
241245
d = super(Text, self).__getstate__()
242246
# remove the cached _renderer (if it exists)
@@ -484,12 +488,18 @@ def set_bbox(self, rectprops):
484488

485489
if rectprops is not None:
486490
props = rectprops.copy()
487-
pad = props.pop('pad', 4) # in points; hardwired default
488-
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())
491+
boxstyle = props.pop("boxstyle", None)
492+
pad = props.pop("pad", None)
493+
if boxstyle is None:
494+
boxstyle = "square"
495+
if pad is None:
496+
pad = 4 # points
497+
pad /= self.get_size() # to fraction of font size
498+
else:
499+
if pad is None:
500+
pad = 0.3
501+
if "pad" not in boxstyle:
502+
boxstyle += ",pad=%0.2f" % pad
493503

494504
bbox_transmuter = props.pop("bbox_transmuter", None)
495505

@@ -530,8 +540,7 @@ def update_bbox_position_size(self, renderer):
530540

531541
posx, posy = trans.transform_point((posx, posy))
532542

533-
x_box, y_box, w_box, h_box = _get_textbox(self, renderer,
534-
with_descent=True)
543+
x_box, y_box, w_box, h_box = _get_textbox(self, renderer)
535544
self._bbox_patch.set_bounds(0., 0., w_box, h_box)
536545
theta = np.deg2rad(self.get_rotation())
537546
tr = mtransforms.Affine2D().rotate(theta)
@@ -547,8 +556,7 @@ def _draw_bbox(self, renderer, posx, posy):
547556
(FancyBboxPatch), and draw
548557
"""
549558

550-
x_box, y_box, w_box, h_box = _get_textbox(self, renderer,
551-
with_descent=True)
559+
x_box, y_box, w_box, h_box = _get_textbox(self, renderer)
552560
self._bbox_patch.set_bounds(0., 0., w_box, h_box)
553561
theta = np.deg2rad(self.get_rotation())
554562
tr = mtransforms.Affine2D().rotate(theta)
@@ -2143,9 +2151,11 @@ def _update_position_xytext(self, renderer, xy_pixel):
21432151
" use 'headlength' to set the head length in points.")
21442152
headlength = d.pop('headlength', 12)
21452153

2146-
stylekw = dict(head_length=headlength / ms,
2147-
head_width=headwidth / ms,
2148-
tail_width=width / ms)
2154+
to_style = self.figure.dpi / (72 * ms)
2155+
2156+
stylekw = dict(head_length=headlength * to_style,
2157+
head_width=headwidth * to_style,
2158+
tail_width=width * to_style)
21492159

21502160
self.arrow_patch.set_arrowstyle('simple', **stylekw)
21512161

0 commit comments

Comments
 (0)