Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit f8744d5

Browse files
committed
Plotting live data - matplotlib series
1 parent a0a9d01 commit f8744d5

File tree

3 files changed

+114
-0
lines changed

3 files changed

+114
-0
lines changed

‎09-Plotting_live_data/data.csv

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
x_val,total_1,total_2
2+
0,1000,1000
3+
1,1004,1001
4+
2,1000,996
5+
3,994,998
6+
4,991,1004
7+
5,996,1009
8+
6,995,1012
9+
7,998,1012
10+
8,1002,1016
11+
9,1002,1012
12+
10,1004,1009
13+
11,1002,1013
14+
12,1004,1012
15+
13,998,1017
16+
14,994,1021
17+
15,998,1027
18+
16,995,1026
19+
17,992,1030
20+
18,997,1027
21+
19,994,1031
22+
20,1002,1027
23+
21,1005,1031
24+
22,1012,1031
25+
23,1012,1031
26+
24,1014,1032
27+
25,1015,1031
28+
26,1014,1034
29+
27,1008,1029
30+
28,1004,1029
31+
29,1006,1034
32+
30,1008,1029
33+
31,1015,1035
34+
32,1009,1030
35+
33,1010,1028
36+
34,1004,1027
37+
35,1003,1025
38+
36,998,1021
39+
37,1004,1023
40+
38,1007,1021
41+
39,1007,1019
42+
40,1010,1015
43+
41,1011,1020
44+
42,1011,1016
45+
43,1010,1022
46+
44,1004,1023
47+
45,998,1020
48+
46,998,1020
49+
47,998,1016

‎09-Plotting_live_data/data_gen.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import csv
2+
import random
3+
import time
4+
5+
x_val = 0
6+
total_1 = 1000
7+
total_2 = 1000
8+
9+
field_names = ['x_val', 'total_1', 'total_2']
10+
11+
with open('data.csv', 'w') as csv_file:
12+
csv_writer = csv.DictWriter(csv_file, fieldnames=field_names)
13+
csv_writer.writeheader()
14+
15+
while True:
16+
with open('data.csv', 'a') as csv_file:
17+
csv_writer = csv.DictWriter(csv_file, fieldnames=field_names)
18+
19+
info = {
20+
"x_val": x_val,
21+
"total_1": total_1,
22+
"total_2": total_2
23+
}
24+
25+
csv_writer.writerow(info)
26+
print(x_val, total_1, total_2)
27+
28+
x_val += 1
29+
total_1 = total_1 + random.randint(-6, 8)
30+
total_2 = total_2 + random.randint(-5, 6)
31+
32+
time.sleep(1)

‎09-Plotting_live_data/demo.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import random
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+
index = count()
13+
14+
15+
def animate(i):
16+
data = pd.read_csv('data.csv')
17+
x = data['x_val']
18+
y1 = data['total_1']
19+
y2 = data['total_2']
20+
21+
plt.cla()
22+
23+
plt.plot(x, y1, label='Channel 1')
24+
plt.plot(x, y2, label='Channel 2')
25+
26+
plt.legend(loc='upper left')
27+
plt.tight_layout()
28+
29+
30+
ani = FuncAnimation(plt.gcf(), animate, interval=1000)
31+
32+
plt.tight_layout()
33+
plt.show()

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /