Skip to content

Instantly share code, notes, and snippets.

@mathisonian
Created Apr 12, 2016
Embed
What would you like to do?
streaming line python
from lightning import Lightning
import numpy as np
import time
from random import randint, random
lgn = Lightning()
session = lgn.create_session()
NUM_LINES = 2
NUM_INITIAL_POINTS = 10
series = [[random() for j in xrange(NUM_INITIAL_POINTS)] for i in xrange(NUM_LINES)]
print series
x = range(NUM_INITIAL_POINTS)
# full manual control
#
viz_full_control = lgn.line(series, index=x)
session.open()
while True:
new_points = [[] for i in xrange(NUM_LINES)]
# choose a line to add the new point to...
line_index = randint(0, NUM_LINES-1)
for i in xrange(len(new_points)):
if i == line_index:
# add a new random point to one of the lines
new_points[i].append(random())
else:
# re-use the last value if there is nothing new
new_points[i].append(series[i][-1])
# update the x-axis values
x.append(x[-1] + random())
for i in xrange(len(series)):
for j in xrange(len(new_points[i])):
series[i].append(new_points[i][j])
viz_full_control.update(series, index=x)
time.sleep(2)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment