您好,登錄后才能下訂單哦!
本文實(shí)例為大家分享了python可視化動(dòng)態(tài)CPU性能監(jiān)控的具體代碼,供大家參考,具體內(nèi)容如下
打算開發(fā)web性能監(jiān)控,以后會(huì)去學(xué)js,現(xiàn)在用matp來補(bǔ)救下,在官網(wǎng)有此類模板,花了一點(diǎn)時(shí)間修改了下,有興趣的可以去官網(wǎng)看看。
基于matplotoilb和psutil,matplotoilb是有名的數(shù)據(jù)數(shù)據(jù)可視化工具,psutil是性能監(jiān)控工具,所以你需要這兩個(gè)環(huán)境,本文不多說環(huán)境的安裝。
以下是代碼:
#!/usr/bin/env python #-*-coding:utf-8 -*- import matplotlib.pyplot as plt import matplotlib.animation as animation import psutil def data_gen(t=0): #設(shè)置xy變量 x = 0 y = 1 while True: y = psutil.cpu_percent(interval=1) #獲取cpu數(shù)值,1s獲取一次。 x += 1 yield x,y def init(): ax.set_xlim(0, 10) #起始x 1-10 ax.set_ylim(0, 100) #設(shè)置y相當(dāng)于0%-100% del xdata[:] del ydata[:] line.set_data(xdata, ydata) return line, fig, ax = plt.subplots() line, = ax.plot([], [], lw=2) #線像素比 ax.grid() xdata, ydata = [], [] def run(data): # update the data t, y = data xdata.append(t) ydata.append(y) xmin, xmax = ax.get_xlim() if t >= xmax: #表格隨數(shù)據(jù)移動(dòng) ax.set_xlim(xmin+10, xmax+10) ax.figure.canvas.draw() line.set_data(xdata, ydata) return line, ani = animation.FuncAnimation(fig, run, data_gen, blit=False, interval=10, repeat=False, init_func=init) plt.show()
下面是效果圖,還有很多地方不完善,以后會(huì)花點(diǎn)時(shí)間完成。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持億速云。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。