In [1]:
%pylab inline
import pylab as pl
import numpy as np
In [2]:
import time
end_time = time.time() + 10
x_bins, y_bins = 50, 25
heatmap = np.zeros((y_bins, x_bins))
time_window = 2
wave_period = 1
class OutOfBounds(Exception):
pass
def binnum(range_min, range_max, bins, value):
width = (range_max - range_min) / float(bins)
bin_num = int((value - range_min) / width)
if 0 <= bin_num < bins:
return bin_num
raise OutOfBounds
def signal():
x = mod(time.time(), time_window)
y = 0.6 * np.sin((2*np.pi)/wave_period*x) + np.random.randn() * 0.2
return (x, y)
while time.time() < end_time:
x, y = signal()
try:
x_bin = binnum(0, time_window, x_bins, x)
y_bin = binnum(-1, 1, y_bins, y)
heatmap[y_bin, x_bin] += 1
except OutOfBounds:
pass
time.sleep(0.001)
pl.pcolor(heatmap, cmap=plt.cm.Blues)
pl.colorbar()
pl.show()
In [ ]: