请考虑直接从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的"Animation"类在一个单独的线程中运行动画.所以我有以下两个问题:
1)如果进程在一个单独的线程中运行,为什么它会受到后续循环的干扰?
2)如何对python说等到动画结束?