import plotly.plotly as py
from plotly.graph_objs import *
import time
import datetime
import numpy as np
import plotly.tools as tls
stream_ids = tls.get_credentials_file()['stream_ids']
stream_ids
[u'p17pxe0idb', u'ilzladc8ro', u'o72o1p08y4', u'81dygs4lct']
def Plotter(): # defines layout and traces for plot
layout = Layout(
title='Bobs environment',
yaxis=YAxis(
domain=[0, 0.5]
),
yaxis2=YAxis(
domain=[0.5, 1]
)
)
trace1 = Scatter(
x=[],
y=[],
name='Room Temp *C',
stream=dict(
token=stream_ids[0],
maxpoints=1000
)
)
trace2 = Scatter(
x=[],
y=[],
name = 'Atmospheric pressure mB',
yaxis = 'y2',
stream=dict(
token=stream_ids[1],
maxpoints=1000
)
)
trace3 = Scatter(
x=[],
y=[],
name = 'Outside Temp *C',
stream=dict(
token=stream_ids[2],
maxpoints=1000
)
)
trace4 = Scatter(
x=[],
y=[],
name = 'Light Level',
yaxis = 'y2',
stream=dict(
token=stream_ids[3],
maxpoints=1000
)
)
data = Data([trace1, trace2, trace3, trace4])
fig = Figure(data=data, layout=layout)
unique_url = py.plot(fig, filename='RoomF (HemiBob)')
#my_data = Data([trace1, trace2, trace3, trace4])
#unique_url = py.plot(my_data, layout, filename='RoomF',auto_open=False,fileopt='extend')
s = py.Stream(stream_ids[0])
q = py.Stream(stream_ids[1])
u = py.Stream(stream_ids[2])
r = py.Stream(stream_ids[3])
return s,q,u,r
N = 100 # number of data points to stream
def dummy_data(n):
return (np.random.random(N) for i in range(n))
# Use some dummy data
data_s, data_q, data_u, data_r = dummy_data(4)
s,q,u,r = Plotter() # get the data streams then open them to plot.ly
s.open()
q.open()
u.open()
r.open()
i = 0
while i<N:
t = datetime.datetime.now().strftime('%H:%M:%S.%f')
s.write(dict(x=t, y=data_s[i]))
q.write(dict(x=t, y=data_q[i]))
u.write(dict(x=t, y=data_u[i]))
r.write(dict(x=t, y=data_r[i]))
time.sleep(0.1)
i += 1
About Plotly
Big thanks to
# CSS styling within IPython notebook
from IPython.core.display import HTML
import urllib2
def css_styling():
url = 'https://raw.githubusercontent.com/plotly/python-user-guide/master/custom.css'
styles = urllib2.urlopen(url).read()
return HTML(styles)
css_styling()