这是一个简单的代码,演示了我想要做什么。该点是细丝的尖端,随机增长和收缩。当缩小发生时,我希望线条也缩小,而不是重新绘制已经可见的更长的线条(基本上在动画的每个步骤中,该点应作为线条的终点可见)。尖端表示细丝是增长还是收缩。
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation
X = [1,2,3,4,5,6,7,7,6,5,4,3,4,5,6,7,8]
Y = [1,2,3,4,5,6,7,7,6,5,4,3,4,5,6,7,8]
fig, ax = plt.subplots(1,1)
ax.set_xlim([0, 20])
ax.set_ylim([-1.1, 20])
graph, = ax.plot([], [], color = ''green)
dot, = ax.plot([], [], 'o', color='green', markersize = 4)
def oj(i):
graph.set_data(X[:i+1],Y[:i+1])
dot.set_data(X[i],Y[i])
anim = animation.FuncAnimation(fig, oj, frames=len(X), interval=1000,repeat
= False)
plt.show()