Skip to content

Commit

Permalink
handle pad defaults; fix sizing of YAArrow substitute
Browse files Browse the repository at this point in the history
  • Loading branch information
efiring committed Jul 20, 2015
1 parent 1e6af77 commit 2eb4313
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 24 deletions.
10 changes: 6 additions & 4 deletions lib/matplotlib/tests/test_text.py
Expand Up @@ -400,12 +400,14 @@ def test_text_with_arrow_annotation_get_window_extent():

@cleanup
def test_arrow_annotation_get_window_extent():
figure = Figure(dpi=100)
dpi = 100
dots_per_point = dpi / 72
figure = Figure(dpi=dpi)
figure.set_figwidth(2.0)
figure.set_figheight(2.0)
renderer = RendererAgg(200, 200, 100)

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

eq_(bbox.width, 50.0)
assert_almost_equal(bbox.height, 10.0 / 0.72)
assert_almost_equal(bbox.height, 10.0 * dots_per_point)
eq_(points[0, 0], 0.0)
eq_(points[0, 1], 50.0 - 5 / 0.72)
eq_(points[0, 1], 50.0 - 5 * dots_per_point)


@cleanup
Expand Down
50 changes: 30 additions & 20 deletions lib/matplotlib/text.py
Expand Up @@ -93,7 +93,8 @@ def get_rotation(rotation):
animated [True | False]
backgroundcolor any matplotlib color
bbox rectangle prop dict plus key 'pad' which is a
pad in points
pad in points; if a boxstyle is supplied, then
pad is instead a fraction of the font size
clip_box a matplotlib.transform.Bbox instance
clip_on [True | False]
color any matplotlib color
Expand Down Expand Up @@ -137,7 +138,7 @@ def get_rotation(rotation):
# function as a method with some refactoring of _get_layout method.


def _get_textbox(text, renderer, with_descent=True):
def _get_textbox(text, renderer):
"""
Calculate the bounding box of the text. Unlike
:meth:`matplotlib.text.Text.get_extents` method, The bbox size of
Expand Down Expand Up @@ -165,10 +166,6 @@ def _get_textbox(text, renderer, with_descent=True):
xt_box, yt_box = min(projected_xs), min(projected_ys)
w_box, h_box = max(projected_xs) - xt_box, max(projected_ys) - yt_box

if not with_descent:
yt_box += d
h_box -= d

tr = mtransforms.Affine2D().rotate(theta)

x_box, y_box = tr.transform_point((xt_box, yt_box))
Expand Down Expand Up @@ -228,7 +225,6 @@ def __init__(self,
self._multialignment = multialignment
self._rotation = rotation
self._fontproperties = fontproperties
self._bbox = None
self._bbox_patch = None # a FancyBboxPatch instance
self._renderer = None
if linespacing is None:
Expand All @@ -237,6 +233,14 @@ def __init__(self,
self.set_rotation_mode(rotation_mode)
self.update(kwargs)

def update(self, kwargs):
"""
Update properties from a dictionary.
"""
bbox = kwargs.pop('bbox', None)
super(Text, self).update(kwargs)
self.set_bbox(bbox) # depends on font properties

def __getstate__(self):
d = super(Text, self).__getstate__()
# remove the cached _renderer (if it exists)
Expand Down Expand Up @@ -484,12 +488,18 @@ def set_bbox(self, rectprops):

if rectprops is not None:
props = rectprops.copy()
pad = props.pop('pad', 4) # in points; hardwired default
boxstyle = props.pop("boxstyle", "square")
# If pad is in the boxstyle string, it will be passed
# directly to the FancyBboxPatch as font units.
if 'pad' not in boxstyle:
boxstyle += ",pad=%0.2f" % (pad / self.get_size())
boxstyle = props.pop("boxstyle", None)
pad = props.pop("pad", None)
if boxstyle is None:
boxstyle = "square"
if pad is None:
pad = 4 # points
pad /= self.get_size() # to fraction of font size
else:
if pad is None:
pad = 0.3
if "pad" not in boxstyle:
boxstyle += ",pad=%0.2f" % pad

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

Expand Down Expand Up @@ -530,8 +540,7 @@ def update_bbox_position_size(self, renderer):

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

x_box, y_box, w_box, h_box = _get_textbox(self, renderer,
with_descent=True)
x_box, y_box, w_box, h_box = _get_textbox(self, renderer)
self._bbox_patch.set_bounds(0., 0., w_box, h_box)
theta = np.deg2rad(self.get_rotation())
tr = mtransforms.Affine2D().rotate(theta)
Expand All @@ -547,8 +556,7 @@ def _draw_bbox(self, renderer, posx, posy):
(FancyBboxPatch), and draw
"""

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

stylekw = dict(head_length=headlength / ms,
head_width=headwidth / ms,
tail_width=width / ms)
to_style = self.figure.dpi / (72 * ms)

stylekw = dict(head_length=headlength * to_style,
head_width=headwidth * to_style,
tail_width=width * to_style)

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

Expand Down

0 comments on commit 2eb4313

Please sign in to comment.