如何重新运行脚本?

2024-10-16 20:41:20 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个名为plot()的函数,它显示一个包含数据的图形。plot()从每隔几秒钟更新一次的文本文件中获取数据。你知道吗

我希望plot()关闭matplotlib窗口,然后每隔t秒运行一次。你知道吗

到目前为止我所拥有的:

plot(t):
    draw plt
    plt.show()
    time.sleep(t)
    plt.close('all')

def rinse_repeat(t, total_time):
    while time.time < total_time
    plot()

rinse_repeat

我怎样才能让它工作?谢谢。你知道吗


Tags: 数据函数图形timeplotmatplotlibshowplt
1条回答
网友
1楼 · 发布于 2024-10-16 20:41:20

这应该对你有用。只需做一个等待并计算秒数的循环:

import time

def plot(t):
    draw plt
    plt.show()
    time.sleep(t)
    plt.close('all')

def rinse_repeat(t, total_time):
    seconds = 0
    while(seconds < total_time):
        time.sleep(t)
        seconds += t
        plot(10) # or whatever you want t to be

相关问题 更多 >