-
Notifications
You must be signed in to change notification settings - Fork 0
/
Graphs.py
120 lines (97 loc) · 3.44 KB
/
Graphs.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
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
from matplotlib import animation
import numpy as np
from Sim import sim
import random
import json
"""
EX
{'cycle': 67, 'pop': 0, 'animals': {'speed': [], 'range': []}}
"""
class graphs:
"""Class to manage graphing the simulation"""
def __init__(self):
self.cycles = 100
print ("Retrieving simulation data...")
with open('data.json') as f:
data = json.load(f)
self.data = data[:-1]
self.data.pop()
def plot_animal_attributes(self):
"""
Plot animal attributes
"""
#Create graph
self.animal_fig, self.animal_ax = plt.subplots(figsize=(5, 5))
#Set labels and axis for graph
self.animal_ax.set_xlabel("Speed")
self.animal_ax.set_ylabel("Range")
self.animal_ax.set_xlim(-1, 11)
self.animal_ax.set_ylim(-1, 11)
x_data = []
y_data = []
sizes = []
scatter = self.animal_ax.scatter(-2, -2, c="#ff0000")
#Create (x, y) coordinates
def create_coordinates(x, y):
coordinates = []
for n, x_coor in enumerate(x):
coor = (x_coor, y[n])
coordinates.append(coor)
return coordinates
#Create sizes for points
def create_sizes(coordinates):
sizes = []
for point in coordinates:
size = coordinates.count(point) * 80
sizes.append(size)
return sizes
def animate(i):
data = self.data[i]
x_data = data["animals"]["speed"]
y_data = data["animals"]["range"]
plt.title(f"Cycle: {data['cycle']} Population: {data['pop']}")
points = create_coordinates(x_data, y_data)
sizes = create_sizes(points)
#Update the graph
scatter.set_offsets(points)
scatter.set_sizes(sizes)
return scatter,
line_animation = animation.FuncAnimation(
self.animal_fig, animate, frames=np.arange(0, len(self.data), 1), interval=0.05*1000,
blit=True, save_count=len(self.data)
)
writergif = animation.PillowWriter(fps=5)
line_animation.save('animals.gif',writer=writergif)
plt.close(self.animal_fig)
def plot_population(self):
self.pop_fig, self.pop_ax = plt.subplots(figsize=(5, 5))
self.pop_ax.set_xlabel("Cycle")
self.pop_ax.set_ylabel("Population")
populations, cycles = [], []
for dataset in self.data:
populations.append(dataset['pop'])
cycles.append(dataset['cycle'])
"""
If the simulation ended when all the animals died,
Add zero to the population
"""
if cycles[-1] + 1 < self.cycles:
populations.append(0)
cycles.append(cycles[-1] + 1)
self.pop_ax.plot(cycles, populations)
#Create the average line
y_average = 0
for i in populations:
y_average += i
y_average = y_average / len(populations)
y_average = [y_average] * len(populations)
x_values = np.arange(0, len(populations), 1)
self.pop_ax.plot(x_values, y_average, label='Mean', linestyle='--')
self.pop_fig.savefig("population.png")
plt.close(self.pop_fig)
if __name__ == '__main__':
g = graphs()
g.plot_animal_attributes()
g.plot_population()