【发布时间】:2020-06-30 19:40:50
【问题描述】:
所以我有一个问题。我有两个脚本正在运行,一个是每秒的 CPU 和时间记录器,用于将 CPU 使用情况记录到文本文件中。另一个是将文本文件读入图形的脚本,但图形不是统一轴,单位不增加,我得到错误的输出视图。
Script1:将 PSU 和时间记录到 txt 文件
import psutil import time
print(str(time.strftime("%H:%M:%S", time.localtime())) + ", " +
str(psutil.cpu_percent(interval=1)))
f = open("example.txt", "a+")
f.write(str(time.strftime("%H:%M:%S", time.localtime())) +
", " + str(psutil.cpu_percent(interval=1)) + " \n")
程序 2:绘制图表
import matplotlib.pyplot as plt
import matplotlib.animation as animation
from matplotlib import style
style.use('fivethirtyeight')
fig = plt.figure() ax1 = fig.add_subplot(1,1,1)
def animate(i):
graph_data = open ('example.txt','r').read()
lines = graph_data.split('\n')
xs = []
ys = []
for line in lines:
if len(line) > 1:
x, y = line.split(',')
xs.append(x)
ys.append(y)
ax1.clear()
ax1.plot(xs, ys)
ani = animation.FuncAnimation(fig, animate, interval=1000) plt.show()
如果可以将其制成一个脚本,那就太好了,但我正在努力学习基础知识。我认为这是写入 txt 文件的字符串问题,但不知道为什么字符串在 txt 文件中很重要。
【问题讨论】:
标签: python python-3.x matlab psutil