Skip to content

Commit 31d8f26

Browse files
anntzerstory645
authored andcommitted
Don't force anncoords to fig coords upon dragging.
Minimal example: from matplotlib import pyplot as plt plt.annotate("foo", (.5, .5), (15, 15), textcoords="offset points", arrowprops={"arrowstyle": "->"}, bbox={"ec": "k"}).draggable() plt.show() Panning the axes shows that the annotation uses offset coordinates: it moves "with" the point it points to. However, (before this patch is applied,) after dragging the annotation somewhere else, it switches to figure coordinates: when panning the axes, the text box stops physically moving; instead it's the arrow that moves to follow the point. The fix actually makes the code simpler...
1 parent 1f7b181 commit 31d8f26

File tree

1 file changed

+3
-21
lines changed

1 file changed

+3
-21
lines changed

lib/matplotlib/offsetbox.py

Lines changed: 3 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1750,30 +1750,12 @@ def __init__(self, annotation, use_blit=False):
17501750

17511751
def save_offset(self):
17521752
ann = self.annotation
1753-
x, y = ann.xyann
1754-
if isinstance(ann.anncoords, tuple):
1755-
xcoord, ycoord = ann.anncoords
1756-
x1, y1 = ann._get_xy(self.canvas.renderer, x, y, xcoord)
1757-
x2, y2 = ann._get_xy(self.canvas.renderer, x, y, ycoord)
1758-
ox0, oy0 = x1, y2
1759-
else:
1760-
ox0, oy0 = ann._get_xy(self.canvas.renderer, x, y, ann.anncoords)
1761-
1762-
self.ox, self.oy = ox0, oy0
1763-
self.annotation.anncoords = "figure pixels"
1764-
self.update_offset(0, 0)
1753+
self.ox, self.oy = ann.get_transform().transform(ann.xyann)
17651754

17661755
def update_offset(self, dx, dy):
17671756
ann = self.annotation
1768-
ann.xyann = self.ox + dx, self.oy + dy
1769-
x, y = ann.xyann
1770-
1771-
def finalize_offset(self):
1772-
loc_in_canvas = self.annotation.xyann
1773-
self.annotation.anncoords = "axes fraction"
1774-
pos_axes_fraction = self.annotation.axes.transAxes.inverted()
1775-
pos_axes_fraction = pos_axes_fraction.transform_point(loc_in_canvas)
1776-
self.annotation.xyann = tuple(pos_axes_fraction)
1757+
ann.xyann = ann.get_transform().inverted().transform(
1758+
(self.ox + dx, self.oy + dy))
17771759

17781760

17791761
if __name__ == "__main__":

0 commit comments

Comments
 (0)