Skip to content

Commit

Permalink
Merge pull request matplotlib#4019 from myshen/annot_neg_coords
Browse files Browse the repository at this point in the history
    BUG : fix negative coords in annotation
  • Loading branch information
tacaswell committed Jan 26, 2015
2 parents b280f7d + e095e65 commit d6e1577
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 6 deletions.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
18 changes: 18 additions & 0 deletions lib/matplotlib/tests/test_text.py
Expand Up @@ -279,8 +279,26 @@ def test_get_rotation_mod360():
for i, j in zip([360., 377., 720+177.2], [0., 17., 177.2]):
assert_almost_equal(text.get_rotation(i), j)


@image_comparison(baseline_images=['text_bboxclip'])
def test_bbox_clipping():
plt.text(0.9, 0.2, 'Is bbox clipped?', backgroundcolor='r', clip_on=True)
t = plt.text(0.9, 0.5, 'Is fancy bbox clipped?', clip_on=True)
t.set_bbox({"boxstyle": "round, pad=0.1"})


@image_comparison(baseline_images=['annotation_negative_coords'],
extensions=['png'])
def test_annotation_negative_coords():
fig = plt.figure()
ax = plt.subplot(1, 1, 1)

ax.annotate("+fpt", (15, 40), xycoords="figure points")
ax.annotate("+fpx", (25, 30), xycoords="figure pixels")
ax.annotate("+apt", (35, 20), xycoords="axes points")
ax.annotate("+apx", (45, 10), xycoords="axes pixels")

ax.annotate("-fpt", (-55, -40), xycoords="figure points")
ax.annotate("-fpx", (-45, -30), xycoords="figure pixels")
ax.annotate("-apt", (-35, -20), xycoords="axes points")
ax.annotate("-apx", (-25, -10), xycoords="axes pixels")
23 changes: 17 additions & 6 deletions lib/matplotlib/text.py
Expand Up @@ -1531,17 +1531,17 @@ def _get_xy(self, renderer, x, y, s):
if s2 == 'data':
y = float(self.convert_yunits(y))

tr = self._get_xy_transform(renderer, s)
tr = self._get_xy_transform(renderer, (x, y), s)
x1, y1 = tr.transform_point((x, y))
return x1, y1

def _get_xy_transform(self, renderer, s):
def _get_xy_transform(self, renderer, xy, s):

if isinstance(s, tuple):
s1, s2 = s
from matplotlib.transforms import blended_transform_factory
tr1 = self._get_xy_transform(renderer, s1)
tr2 = self._get_xy_transform(renderer, s2)
tr1 = self._get_xy_transform(renderer, xy, s1)
tr2 = self._get_xy_transform(renderer, xy, s2)
tr = blended_transform_factory(tr1, tr2)
return tr

Expand Down Expand Up @@ -1590,7 +1590,17 @@ def _get_xy_transform(self, renderer, s):
# bbox0 = self._get_bbox(renderer, bbox)

if bbox0 is not None:
xy0 = bbox0.bounds[:2]
x, y = xy
bounds = bbox0.extents
if x < 0:
x0 = bounds[2]
else:
x0 = bounds[0]
if y < 0:
y0 = bounds[3]
else:
y0 = bounds[1]
xy0 = (x0, y0)
elif bbox_name == "offset":
xy0 = self._get_ref_xy(renderer)

Expand Down Expand Up @@ -1950,7 +1960,8 @@ def _update_position_xytext(self, renderer, xy_pixel):
patch.
"""
# generate transformation,
self.set_transform(self._get_xy_transform(renderer, self.anncoords))
self.set_transform(self._get_xy_transform(
renderer, self.xy, self.anncoords))

ox0, oy0 = self._get_xy_display()
ox1, oy1 = xy_pixel
Expand Down

0 comments on commit d6e1577

Please sign in to comment.