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不是很熟悉。

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