python - 连续绘制日期和数据 matplotlib

标签 python matplotlib plot

我编写了以下代码,用于从文件中收集数据、为其添加时间戳并绘制它。我有以下工作代码:

temp_data=0
x=[datetime.now() + timedelta(hours=-i) for i in range(5)]
y=[temp_data+i for i in range(len(x))]
while True:

    f=open("/sys/class/thermal/thermal_zone0/temp", "r")

    #timestamp the data
    temp_time=datetime.now()

    #read the data in the file to a variable and divide by 1000 to get correct value
    temp_data=int(f.readlines()[0].strip())/1000
    x=x[1:]
    x.append(temp_time)
    y=y[1:]
    y.append(temp_data)
    plt.gcf().autofmt_xdate()
    plt.plot(x,y)
    plt.show()
    sleep(5)


print "Good Bye, Exiting the Program"
    #close file after reading
    f.close()

现在发生的事情是,绘图得到显示,我必须关闭绘图窗口,以便下一组数据出现在绘图上。

我想进一步扩展它,其中我的绘图在读取文件并为其添加时间戳后连续绘制数据。

提前致谢。

最佳答案

您可以打开一个图形并按住它。

temp_data=0
x=[datetime.now() + timedelta(hours=-i) for i in range(5)]
y=[temp_data+i for i in range(len(x))]
plt.figure() ###### Create figure
while True:

    f=open("/sys/class/thermal/thermal_zone0/temp", "r")

    #timestamp the data
    temp_time=datetime.now()

    #read the data in the file to a variable and divide by 1000 to get correct value
    temp_data=int(f.readlines()[0].strip())/1000
    x=x[1:]
    x.append(temp_time)
    y=y[1:]
    y.append(temp_data)
    plt.hold(True) ##### Hold it.
    plt.gcf().autofmt_xdate()
    plt.plot(x,y)
    plt.show()
    sleep(5)


print "Good Bye, Exiting the Program"
    #close file after reading
    f.close()

关于python - 连续绘制日期和数据 matplotlib,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39320890/

相关文章:

r - 如何更改 visreg 中交互图的颜色和线型

matlab - 如何在 Matlab 中使用不同颜色绘制由 15 个数据集和 5 个不同变量组成的 9x75 矩阵?

python - 将映射函数应用到 ndarray 的每个成员,并以索引作为参数

python: TypeError: 'str' 对象不可调用

python - Discord 金钱机器人将用户 ID 保存在 json 文件中。当 Not 重新启动时,它会为每个人创建一个新的(但相同的)ID

python - 以全尺寸显示标签中的图像

python - 用频率表绘制频率分布/直方图

python - 在 python 中绘制二进制数据

python - 绘制故障线而不将每条线连接在一起

python - matplotlib 维恩图,6 个圆圈