我正在使用 matplotlib 动画探索实时图表。目前,我只是将一个随机整数添加到一个数组中,并根据新数字更新我的绘图。问题是我似乎得到了情节的额外价值。
我有以下代码:
import matplotlib.pyplot as plt
import matplotlib.animation as animation
import random
fig = plt.figure()
ax1 = fig.add_subplot(1, 1, 1)
xs = []
ys = []
def animate(i, xs, ys):
x = i + 1
y = random.randint(1, 50)
print '{}: {}, {}'.format(i, x, y)
xs.append(x)
ys.append(y)
ax1.clear()
ax1.plot(xs, ys)
ani = animation.FuncAnimation(fig,
animate,
frames = 10,
fargs = (xs, ys),
repeat = False)
plt.show()
我只想绘制 10 个值,所以我frames = 10
在FuncAnimate
调用中设置。但是,打印语句输出第 11 个值:
所以很明显正在生成 11 帧,而不是 10 帧。查看 FuncAnimate 的文档,我看不出发生这种情况的原因。
谁能告诉我我错过了什么?
谢谢!