Skip to content

Commit cab1c1b

Browse files
committed
Added examples of charting live data using MATPLOTLIB
1 parent 78fa1e5 commit cab1c1b

File tree

7 files changed

+185
-2
lines changed

7 files changed

+185
-2
lines changed

matplotlib/Live_Updating_Chart.ipynb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58165,9 +58165,9 @@
5816558165
"name": "python",
5816658166
"nbconvert_exporter": "python",
5816758167
"pygments_lexer": "ipython3",
58168-
"version": "3.7.3"
58168+
"version": "3.8.2"
5816958169
}
5817058170
},
5817158171
"nbformat": 4,
58172-
"nbformat_minor": 1
58172+
"nbformat_minor": 4
5817358173
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import csv
2+
import random
3+
import time
4+
5+
x_value = 0
6+
total_1 = 1000
7+
total_2 = 1000
8+
9+
fieldnames = ["x_value", "total_1", "total_2"]
10+
11+
12+
with open('data.csv', 'w') as csv_file:
13+
csv_writer = csv.DictWriter(csv_file, fieldnames=fieldnames)
14+
csv_writer.writeheader()
15+
16+
while True:
17+
18+
with open('data.csv', 'a') as csv_file:
19+
csv_writer = csv.DictWriter(csv_file, fieldnames=fieldnames)
20+
21+
info = {
22+
"x_value": x_value,
23+
"total_1": total_1,
24+
"total_2": total_2
25+
}
26+
27+
csv_writer.writerow(info)
28+
print(x_value, total_1, total_2)
29+
30+
x_value += 1
31+
total_1 = total_1 + random.randint(-6, 8)
32+
total_2 = total_2 + random.randint(-5, 6)
33+
34+
time.sleep(1)
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import csv
2+
import psutil
3+
import random
4+
import time
5+
6+
x_value = 0
7+
8+
fieldnames = ["x_value", "cpu_perc"]
9+
10+
11+
with open('data.csv', 'w') as csv_file:
12+
csv_writer = csv.DictWriter(csv_file, fieldnames=fieldnames)
13+
csv_writer.writeheader()
14+
15+
while True:
16+
17+
with open('data.csv', 'a') as csv_file:
18+
csv_writer = csv.DictWriter(csv_file, fieldnames=fieldnames)
19+
y_value = psutil.cpu_percent()
20+
21+
info = {
22+
"x_value": x_value,
23+
"cpu_perc": y_value
24+
}
25+
26+
csv_writer.writerow(info)
27+
print(x_value, y_value)
28+
29+
x_value += 1
30+
31+
time.sleep(1)
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
# Another way to do it without clearing the Axis
2+
from itertools import count
3+
import pandas as pd
4+
import matplotlib.pyplot as plt
5+
from matplotlib.animation import FuncAnimation
6+
7+
plt.style.use('fivethirtyeight')
8+
9+
x_vals = []
10+
y_vals = []
11+
12+
plt.plot([], [], label='Channel 1')
13+
plt.plot([], [], label='Channel 2')
14+
15+
16+
def animate(i):
17+
data = pd.read_csv('data.csv')
18+
x = data['x_value']
19+
y1 = data['total_1']
20+
y2 = data['total_2']
21+
22+
ax = plt.gca()
23+
line1, line2 = ax.lines
24+
25+
line1.set_data(x, y1)
26+
line2.set_data(x, y2)
27+
28+
xlim_low, xlim_high = ax.get_xlim()
29+
ylim_low, ylim_high = ax.get_ylim()
30+
31+
# Run this if you want xlim to be fixed at zero
32+
# ax.set_xlim(xlim_low, (x.max() + 5))
33+
34+
ax.set_xlim(left=max(0, x.max()-50), right=x.max()+50)
35+
36+
y1max = y1.max()
37+
y2max = y2.max()
38+
current_ymax = y1max if (y1max > y2max) else y2max
39+
40+
y1min = y1.min()
41+
y2min = y2.min()
42+
current_ymin = y1min if (y1min < y2min) else y2min
43+
44+
ax.set_ylim((current_ymin - 5), (current_ymax + 5))
45+
46+
47+
ani = FuncAnimation(plt.gcf(), animate, interval=1000)
48+
49+
plt.legend()
50+
plt.tight_layout()
51+
plt.show()
52+
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# Another way to do it without clearing the Axis
2+
# pip install mplcyberpunk
3+
from itertools import count
4+
import mplcyberpunk
5+
import pandas as pd
6+
import matplotlib.pyplot as plt
7+
from matplotlib.animation import FuncAnimation
8+
9+
plt.style.use("cyberpunk")
10+
11+
plt.plot([], [], marker='o', label='CPU %')
12+
13+
14+
def animate(i):
15+
data = pd.read_csv('data.csv')
16+
x = data['x_value']
17+
y = data['cpu_perc']
18+
19+
ax = plt.gca()
20+
line = ax.lines
21+
22+
line[0].set_data(x, y)
23+
line[0].set_linewidth(2)
24+
25+
xlim_low, xlim_high = ax.get_xlim()
26+
ylim_low, ylim_high = ax.get_ylim()
27+
28+
ax.set_xlim(left=max(0, x.max()-50), right=x.max()+5)
29+
30+
# Run this line instead if you want the xlim to be fixed at zero
31+
# ax.set_xlim(xlim_low, (x.max() + 5))
32+
33+
current_ymin = y.min()
34+
current_ymax = y.max()
35+
36+
ax.set_ylim((current_ymin - 5), (current_ymax + 5))
37+
38+
mplcyberpunk.add_underglow()
39+
40+
# Unfortunately, this effect does not work when using animation
41+
# Set a higher linewidth as a workaround
42+
#mplcyberpunk.make_lines_glow()
43+
44+
45+
ani = FuncAnimation(plt.gcf(), animate, interval=1000)
46+
47+
plt.legend()
48+
plt.tight_layout()
49+
plt.show()

oop/args_kwargs.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
def function(*args):
2+
print(type(args)) # args is of type tuple
3+
for i in args:
4+
print(i)
5+
6+
function(1, 2, 3, 4)
7+
8+
9+
def function(**kwargs):
10+
print(type(kwargs)) # kwargs is of type dict
11+
for i in kwargs:
12+
print(i, kwargs[i])
13+
14+
function(a=1, b=2, c=3, d=4)

oop/isinstance_issubclass.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
isinstance(object, Class)
2+
3+
issubclass(SubClass, Class)

0 commit comments

Comments
 (0)