您可以返回所有从更新功能更改的对象。因此,由于注释已更改,因此您还应该返回它的位置:
line.set_data(newData)annotation = plt.annotate('A0', xy=(newData[0][0],newData[1][0]))return line, annotation您可以
matplotlib在本教程中阅读有关动画的更多信息
您还应该指定
init函数,以便
FuncAnimation在第一次更新时重画时知道要从图中删除哪些元素。因此,完整的示例将是:
import numpy as npimport matplotlib.pyplot as pltimport matplotlib.animation as animation# Create initial datadata = np.array([[1,2,3,4,5], [7,4,9,2,3]])# Create figure and axesfig = plt.figure()ax = plt.axes(xlim=(0, 20), ylim=(0, 20))# Create initial objectsline, = ax.plot([], [], 'r-')annotation = ax.annotate('A0', xy=(data[0][0], data[1][0]))annotation.set_animated(True)# Create the init function that returns the objects# that will change during the animation processdef init(): return line, annotation# Create the update function that returns all the# objects that have changeddef update(num): newData = np.array([[1 + num, 2 + num / 2, 3, 4 - num / 4, 5 + num], [7, 4, 9 + num / 3, 2, 3]]) line.set_data(newData) # This is not working i 1.2.1 # annotation.set_position((newData[0][0], newData[1][0])) annotation.xytext = (newData[0][0], newData[1][0]) return line, annotationanim = animation.FuncAnimation(fig, update, frames=25, init_func=init, interval=200, blit=True)plt.show()


