【发布时间】:2020-07-01 15:00:28
【问题描述】:
我正在使用 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 个值,所以我在 FuncAnimate 调用中设置了 frames = 10。但是,打印语句输出的是第 11 个值:
所以很明显正在生成 11 个帧,而不是 10 个。查看FuncAnimate 的文档,我看不出发生这种情况的原因。
谁能告诉我我错过了什么?
谢谢!
【问题讨论】:
标签: python matplotlib animation random matplotlib-animation