-
Notifications
You must be signed in to change notification settings - Fork 0
/
dataplot.py
128 lines (107 loc) · 3.56 KB
/
dataplot.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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
import matplotlib.pyplot as plt
import matplotlib.animation as animation
from matplotlib import style
import numpy as np
import math
def truncate(number, decimals=0):
"""
Returns a value truncated to a specific number of decimal places.
"""
if not isinstance(decimals, int):
raise TypeError("decimal places must be an integer.")
elif decimals < 0:
raise ValueError("decimal places has to be 0 or more.")
elif decimals == 0:
return math.trunc(number)
factor = 10.0 ** decimals
return math.trunc(number * factor) / factor
style.use('fivethirtyeight')
fig, axs = plt.subplots(nrows=2, ncols=1, figsize=(5, 10))
filepath = "images/foodvstime.txt"
widthbar = .3
def animate(i):
graph_data = open(filepath, 'r').read()
lines = graph_data.split('\n')
t = []
food1 = []
food2 = []
food3 = []
lifea = []
lifeb = []
lifec = []
lifed = []
lifee = []
lifef = []
lifeg = []
lifeh = []
lifej = []
lifek = []
ants = [7, 8, 5, 5, 7, 5, 4, 7, 9, 7]
smellpowers = [7, 8, 5, 5, 7, 5, 4, 7, 9, 7]
life = [20, 15.9, 10, 40, 30, 20, 35, 25, 15.6, 40]
foodearned = []
antsx = np.arange(len(ants)) # the label locations
for line in lines:
if len(line) > 1:
y1, y2, y3, la, lb, lc, ld, le, lf, lg, lh, lj, lk, a, b, c, d, e, f, g, h, j, k, x = line.split(
',')
t.append(float(x))
food1.append(truncate(float(y1), 3))
food2.append(truncate(float(y2), 3))
food3.append(truncate(float(y3), 3))
lifea.append(float(la))
lifeb.append(float(lb))
lifec.append(float(lc))
lifed.append(float(ld))
lifee.append(float(le))
lifef.append(float(lf))
lifeg.append(float(lg))
lifeh.append(float(lh))
lifej.append(float(lj))
lifek.append(float(lk))
foodearned.clear()
foodearned.append(a)
foodearned.append(b)
foodearned.append(c)
foodearned.append(d)
foodearned.append(e)
foodearned.append(f)
foodearned.append(g)
foodearned.append(h)
foodearned.append(j)
foodearned.append(k)
'''axs[1, 1].clear()
axs[1, 0].clear()'''
axs[0].clear()
axs[1].clear()
'''axs[0, 1].clear()'''
# plot time signal:
axs[0].set_title("Time vs Food Amount")
axs[0].plot(t, food1, t, food2, t, food3)
axs[0].set_xlabel("Time")
axs[0].set_ylabel("Food Amount")
axs[1].set_title("Ant's Life")
axs[1].plot(t, lifea, t, lifeb, t, lifec, t, lifed, t,
lifee, t, lifef, t, lifeg, t, lifeh, t, lifej, t, lifek)
axs[1].set_xlabel("Time")
axs[1].set_ylabel("Life")
'''
axs[1, 0].bar(antsx, foodearned, widthbar, label='Amount eaten')
# Add some text for labels, title and custom x-axis tick labels, etc.
axs[1, 0].set_ylabel('Food Earned')
axs[1, 0].set_title('Smellpower vs Foodearned')
axs[1, 0].set_xticks(antsx)
axs[1, 0].set_xticklabels(ants)
axs[1, 0].legend()
axs[1, 1].bar(antsx, foodearned, widthbar, label='Food Earned')
# Add some text for labels, title and custom x-axis tick labels, etc.
axs[1, 1].set_ylabel('Food Earned')
axs[1, 1].set_title('Initial Life vs Foodearned')
axs[1, 1].set_xticks(antsx)
axs[1, 1].set_xticklabels(ants)
axs[1, 1].legend()
'''
fig.tight_layout()
ani = animation.FuncAnimation(fig, animate, interval=100)
fig.tight_layout()
plt.show()