-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.py
39 lines (38 loc) · 1.05 KB
/
test.py
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
import matplotlib; matplotlib.use("TKAgg")
import matplotlib.pyplot as plt
import numpy as np
from matplotlib.animation import FuncAnimation
import psutil
import collections
# function to update the data
def my_function(i):
# get data
cpu.popleft()
cpu.append(psutil.cpu_percent())
ram.popleft()
ram.append(psutil.virtual_memory().percent)
# clear axis
ax.cla()
ax1.cla()
# plot cpu
ax.plot(cpu)
ax.scatter(len(cpu)-1, cpu[-1])
ax.text(len(cpu)-1, cpu[-1]+2, "{}%".format(cpu[-1]))
ax.set_ylim(0,100)
# plot memory
ax1.plot(ram)
ax1.scatter(len(ram)-1, ram[-1])
ax1.text(len(ram)-1, ram[-1]+2, "{}%".format(ram[-1]))
ax1.set_ylim(0,100)
# start collections with zeros
cpu = collections.deque(np.zeros(10))
ram = collections.deque(np.zeros(10))
# define and adjust figure
fig = plt.figure(figsize=(12,6), facecolor='#DEDEDE')
ax = plt.subplot(121)
ax1 = plt.subplot(122)
ax.set_facecolor('#DEDEDE')
ax1.set_facecolor('#DEDEDE')
# animate
ani = FuncAnimation(fig, my_function, interval=1000)
plt.show()