There seems to be a problem with the draggable annotation code when
the annotation is just clicked, and not dragged. If you run the
import matplotlib.pyplot as plt
fig = plt.figure()
ax = fig.add_subplot(111)
ann = ax.annotate('local max', xy=(2, 1), xytext=(3,
And click (just a single click) the annotation (the words "local
max"), then resize your window a little to force a redraw. The
annotation flips to the bottom left corner.
The fix is in offsetbox.py, DraggableAnnotation.save_offset. If you
add a call to update_offset() as the last call of that routine,
everything is OK again. I think that finalize_offset is assuming that
self.annotation.text is in pixels before it starts working with it,
but when update_offset fails to get called (as the case with a click),
self.annotation.xytext is still in axes fraction coordinates.
ann = self.annotation
x, y = ann.xytext
if isinstance(ann.textcoords, tuple):
xcoord, ycoord = ann.textcoords
x1, y1 = ann._get_xy(self.canvas.renderer, x, y, xcoord)
x2, y2 = ann._get_xy(self.canvas.renderer, x, y, ycoord)
ox0, oy0 = x1, y2
ox0, oy0 = ann._get_xy(self.canvas.renderer, x, y, ann.textcoords)
self.ox, self.oy = ox0, oy0
self.annotation.textcoords = "figure pixels"
self.update_offset(0.0,0.0) # <----------- add this!
It might be sufficient to just set self.annotation.xytext to
self.ox,self.oy instead of calling update_offset like I did above.