0
我有一个动画python图形。虽然我可以在计数器每次除以100时添加注释(箭头),但我无法弄清楚如何维持箭头(或将其附加到图)(请参阅我的屏幕截图)。目前,add_annotation
的作品,但只有1秒,然后消失(它应该继续看到至少10秒左右)。带matplotlib的动画图形 - 无法维持/追加注释
转述从here
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation
fig = plt.figure()
ax1 = fig.add_subplot(1,1,1)
z = np.random.normal(0,1,255)
u = 0.1
sd = 0.3
counter = 0
price = [100]
t = [0]
def add_annotation(annotated_message,value):
plt.annotate(annotated_message,
xy = (counter, value), xytext = (counter-5, value+20),
textcoords = 'offset points', ha = 'right', va = 'bottom',
bbox = dict(boxstyle = 'round,pad=0.1', fc = 'yellow', alpha = 0.5),
arrowprops = dict(arrowstyle = '->', connectionstyle = 'arc3,rad=0'))
def getNewPrice(s,mean,stdev):
r = np.random.normal(0,1,1)
priceToday = s + s*(mean/255+stdev/np.sqrt(225)*r)
return priceToday
def animate(i):
global t,u,sd,counter
x = t
y = price
counter += 1
x.append(counter)
value = getNewPrice(price[counter-1],u,sd)
y.append(value)
ax1.clear()
if counter%100==0:
print "ping"
add_annotation("100",value)
plt.plot(x,y,color="blue")
ani = animation.FuncAnimation(fig,animate,interval=20)
plt.show()