python psutil模块后续。监控cpu使用率并使用matplotlib生成散点图

之前说过,会有pstuil后续,今天奉上。
求大神指点

#__author__ = 'chubby_superman'
#_*_coding=utf-8 _*_

import psutil
import time
import matplotlib.dates
import matplotlib.pyplot as plt
def cpu_count():
    print('-----------------------------cpu信息---------------------------------------')
    print(u"物理CPU个数: %s" % psutil.cpu_count(logical=False))
    print(u"CPU核心总数: %s" % psutil.cpu_count())
    cpu1=psutil.cpu_percent(1)
    cpu = (str(cpu1))+ '%'
    sys=time.localtime(time.time())
    sys_time = time.strftime("%H:%M:%S",sys)
    print(u"cup使用率: %s" % cpu)
    cpu_list=["cup使用率",cpu1,sys_time]
    return cpu_list
def a():
    while 1:
        yield cpu_count()
if __name__=="__main__":
    fig=plt.figure(figsize=(10,5))
    plt.xlabel("time")
    plt.xticks(rotation=60)
    plt.ylim(0,100)
    plt.yticks([a for a in range(101) if a%5==0])
    plt.ylabel("data")
    plt.title("test")
    plt.grid(True) #添加网格
    plt.ion()  #interactive mode on
    try:
        while 1:
            data = next(a())
            plt.plot(data[2],data[1],linewidth = '1', label = "cpu",marker = 'o')
            plt.pause(0.1)
    except Exception as e:
        plt.ioff()
    plt.show()

生成的散点图还有一些问题,找了好久我也未能解决,对于matplotlib不是很熟悉。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章