-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHeatmap.py
51 lines (40 loc) · 1.53 KB
/
Heatmap.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
import numpy as np
import matplotlib.pyplot as plt
from collections import deque
class Heatmap:
# multiple Agents
# current locations
# input current positions
def __init__(self, keep_track_of_steps=200, style='Blues', fixed_color_scheme=False):
self.maps = deque()
self.keep_track_of_steps = keep_track_of_steps
self.style = style
self.fixed_color_scheme = fixed_color_scheme
def add_map(self, map):
self.maps.append(map)
if len(self.maps) > self.keep_track_of_steps:
self.maps.popleft()
def get_heatmap(self, num_steps=None):
if num_steps is None:
num_steps = self.keep_track_of_steps
if num_steps > self.keep_track_of_steps:
raise Exception("can't show map, too many steps")
num_steps = min(len(self.maps),num_steps)
sum_map = sum([self.maps[-(i+1)] for i in range(num_steps)])
return sum_map
def show_heatmap(self, num_steps=None):
if num_steps is None:
num_steps = self.keep_track_of_steps
if num_steps > self.keep_track_of_steps:
raise Exception("can't show map, too many steps")
heat_map = self.get_heatmap(num_steps).astype(float)
heat_map /= float(num_steps)
# Plot the heatmap
fig, ax = plt.subplots()
im = ax.imshow(heat_map,cmap=self.style)
if self.fixed_color_scheme:
im.set_clim(0,1)
# Create colorbar
cbar = ax.figure.colorbar(im, ax=ax)
fig.tight_layout()
plt.show()