【发布时间】:2016-03-29 16:36:48
【问题描述】:
考虑以下直接取自 Matplotlib 文档的代码:
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation
import time # optional for testing only
import cv2 # optional for testing only
fig = plt.figure()
def f(x, y):
return np.sin(x) + np.cos(y)
x = np.linspace(0, 2 * np.pi, 120)
y = np.linspace(0, 2 * np.pi, 100).reshape(-1, 1)
im = plt.imshow(f(x, y), animated=True)
def updatefig(*args):
global x, y
x += np.pi / 15.
y += np.pi / 20.
im.set_array(f(x, y))
return im,
ani = animation.FuncAnimation(fig, updatefig, interval=50, blit=True)
plt.show()
这在我的系统上运行良好。现在,尝试将以下代码附加到上面的代码中:
while True:
#I have tried any of these 3 commands, without success:
pass
#time.sleep(1)
#cv2.waitKey(10)
会发生什么是程序冻结。显然,Matplotlib 的“动画”类在单独的线程中运行动画。所以我有以下两个问题:
1)如果进程在单独的线程中运行,为什么会被后续循环干扰?
2) 如何让 python 等到动画结束?
【问题讨论】:
-
我在这里看不到重复,最多只是一些远的提示。您能否解释一下您引用的主题中我的问题的答案在哪里?
标签: python multithreading animation matplotlib