5
我试图创建生动的情节,其更新为可用的数据交互。现场matplotlib阴谋
import os,sys
import matplotlib.pyplot as plt
import time
import random
def live_plot():
fig = plt.figure()
ax = fig.add_subplot(111)
ax.set_xlabel('Time (s)')
ax.set_ylabel('Utilization (%)')
ax.set_ylim([0, 100])
ax.set_xlim(left=0.0)
plt.ion()
plt.show()
start_time = time.time()
traces = [0]
timestamps = [0.0]
# To infinity and beyond
while True:
# Because we want to draw a line, we need to give it at least two points
# so, we pick the last point from the previous lists and append the
# new point to it. This should allow us to create a continuous line.
traces = [traces[-1]] + [random.randint(0, 100)]
timestamps = [timestamps[-1]] + [time.time() - start_time]
ax.set_xlim(right=timestamps[-1])
ax.plot(timestamps, traces, 'b-')
plt.draw()
time.sleep(0.3)
def main(argv):
live_plot()
if __name__ == '__main__':
main(sys.argv)
上述代码有效。然而,我无法与plt.show()
产生窗口交互我如何可以绘制实时的数据,同时还能够与绘图窗口进行交互?
我很困惑..如果循环仅在'plt.pause()'期间运行,那么我不必指定一个大的暂停时间段来确保它可以接收和处理所有的命令鼠标拖动 - 拉伸/缩放/等)?你能修改我的例子来描述你的意思吗? –
让你的例子就像现在一样,用'plt.pause(0.3)'替换'time.sleep(0.3)'。你不需要很长的停顿时间,因为你有一个短暂的停顿时间,无休止地继续。如果你的代码在暂停命令之间花费的时间很少,那么一切都应该平稳运行。 – kazemakase