我正在尝试编写一个每 0.5 秒更新一次的示例图表。
代码是这样的:
import matplotlib.pyplot as plt
import numpy as np
import time
# Some example data to display
x = np.linspace(0, 2 * np.pi, 400)
y = np.sin(x ** 2)
fig, axs = plt.subplots(2, 2)
i = 5
for row in x:
axs[0, 0].plot(x[len(x) - i:], y[len(y) - i:])
axs[0, 0].set_title("main")
axs[1, 0].plot(x, y ** 2)
axs[1, 0].set_title(f"shares {i} with main")
axs[1, 0].sharex(axs[0, 0])
axs[0, 1].plot(x + 1, y + 1)
axs[0, 1].set_title("unrelated")
axs[1, 1].plot(x + 2, y + 2)
axs[1, 1].set_title("also unrelated")
fig.canvas.draw()
fig.canvas.flush_events()
fig.tight_layout()
i += 1
plt.show()
print('THIS NEVER PRINTS - IT SEEMS TO BLOCK ON THE show() CALL ABOVE')
time.sleep(0.5)
如果您运行它,将显示图表。但是那个 show()
调用似乎阻塞了,并且循环永远不会执行来更新图表。
如何更新?