How to shift matplotlib annotations after creating them?

Viewed 776

I have the following code to produce a arrow using ax.annotate():

import numpy as np
import matplotlib.pyplot as plt

fig, ax = plt.subplots()

t = np.arange(0.0, 5.0, 0.01)
s = np.cos(2*np.pi*t)
line, = ax.plot(t, s, lw=2)

at = ax.annotate('local max', xy=(2, 1), xytext=(3, 1.5),
        arrowprops=dict(facecolor='black', shrink=0.05),
        )
ax.set_ylim(-2, 2)

enter image description here

Here, I just want to shift the arrows up/down or left/right afterwards. Is there any way to access "the coordinates" to make the shifting possible via the returned at.

I have tried at.set_x() and at.set_y(), but both only change one end of the arrow. Updating the position of annotation arrows afterwards is the key here.

1 Answers

Unfortunately, both at.set_position() and at.set_x() / at.set_y() change the text position. However, you can change the xy-coordinates directly:

at.xy = (new_x,new_y)
Related