Python - 基于计时器更新绘图

标签 python matplotlib timer

我有一个二维数组列表,我使用 matplotlib 中的 slider 绘制它们。
我在绘图中添加了一些控制按钮,因此我想单击一个 play 按钮,并且绘图会随着时间间隔而变化。
这是按钮调用的函数:

def play(mouse_event):    
    for x in range(len(listOfMoments)):
        image.set_data(listOfMoments[x]) 
        time.sleep(0.5)
        print(x)

print(x) 显示 x 正常增加,但是,在增量完成后,它仅绘制列表的最后一个数组。 我的问题是:我怎样才能让它像预期的那样一一绘制数组?

我还应该提到,我尝试了 while 循环并得到了相同的结果。

最佳答案

每次循环更改数据后,您需要告诉后端更新图形:

fig.canvas.draw()

这是documentation reference

如果您没有对 Figure 对象的引用,您可以使用 fig = plt.gcf() 检索它。

关于Python - 基于计时器更新绘图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45450847/

相关文章:

python - 从一些列标题中删除前 x 个字符

python - SK学习: Losing names of columns when using TfidfVectorizer

python - 如何将错误消息附加到 django 中的 form.non_field_errors?

python - 如何制作不同大小、颜色、位置的散点图?

Swift 计时器检查 TouchesEnded

python - 少量更改后,Python YouTube Gdata API损坏

python - 在第二台显示器上更新/刷新 matplotlib 图

.net - 在 WPF 中处理时间驱动事件的最佳方式是什么

python - 循环中 "time.sleep"与 "threading.Timer"的资源使用情况

python - Matplotlib:子图的高度相同