本文實例為大家分享了python可視化動態CPU性能監控的具體代碼,供大家參考,具體內容如下
打算開發web性能監控,以后會去學js,現在用matp來補救下,在官網有此類模板,花了一點時間修改了下,有興趣的可以去官網看看。
基于matplotoilb和psutil,matplotoilb是有名的數據數據可視化工具,psutil是性能監控工具,所以你需要這兩個環境,本文不多說環境的安裝。
以下是代碼:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
|
#!/usr/bin/env python #-*-coding:utf-8 -*- import matplotlib.pyplot as plt import matplotlib.animation as animation import psutil def data_gen(t = 0 ): #設置xy變量 x = 0 y = 1 while True : y = psutil.cpu_percent(interval = 1 ) #獲取cpu數值,1s獲取一次。 x + = 1 yield x,y def init(): ax.set_xlim( 0 , 10 ) #起始x 1-10 ax.set_ylim( 0 , 100 ) #設置y相當于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: #表格隨數據移動 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() |
下面是效果圖,還有很多地方不完善,以后會花點時間完成。
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。
原文鏈接:https://blog.csdn.net/qq_33083319/article/details/53672278