Hello everyone, this is my first post as Iâm very new to plotly.
I followed the tutorial to create a graph of a data stream; reproducing the âGetting startedâ example worked fine (although i had to change the py.iplot() into py.plot()).
I made some small modifications to the working example code to get the Y-axis value from a text file on my local drive. It does manage to plot the value written in my text file on the graph and even update it as I modify the value in the text file, but it behaves differently than the graph produced by the example code for a streamed plotly graph. I include both my code for the âExampleâ graph and my code for the âData from local text fileâ graph; and images of the different behaviors.
The first two images show the Plot and Data produced by the âExampleâ code and the last two for the âData from local text fileâ code. : http://imgur.com/a/ugo6m
The interesting thing here is that in the first case (Example), the updated value of Y is shown on a new line in the Data tab. This is not the case in the second case (Data from local text file). In the second case, the Y value is updated, but always takes the place of the first line. Instead of adding a new Y point and storing the previous one, it just constantly modifies the first value that Y received. I think the problem comes from there.
Hereâs a link for both codes, theyâre short and only the last few lines matter, as I suppose the problem comes from there since theyâre the only difference between both codes. I tried different working expressions to read the value from the text file ("with open(âdata.txtâ, ârâ)) but nothing does it. Does anyone know how to make it work properly?
(careful, both codes run an infinite loop)
The âExampleâ: http://pastebin.com/6by30ANs
"Data from local text file": see below
Thanks in advance for your time,
PS: I could only put two links in this post, so I had to put my most relevant code directly in this post:
import plotly.plotly as py
import plotly.tools as tls
import plotly.graph_objs as go
import datetime
import time
tls.set_credentials_file(username=âlo.dewaeleâ, stream_ids = [âaqwoq4i2orâ], api_key=âPNASXMZQmLmAVLtthYq2â)
stream_ids = tls.get_credentials_file()[âstream_idsâ]
stream_id = stream_ids[0]
stream_1 = dict(token=stream_id, maxpoints=20)
trace1 = go.Scatter(
x=[],
y=[],
mode=âlines+markersâ,
stream=stream_1
)
data = go.Data([trace1])
layout = go.Layout(title=âTime Seriesâ)
fig = go.Figure(data=data, layout=layout)
py.plot(fig, filename=âstreamâ)
s = py.Stream(stream_id)
s.open()
time.sleep(1)
while True: #Infinite loop, careful
graphdata = open(âgraphdata.txtâ, ârâ) #open file in read mode
y = [graphdata.readline()] #read the first line of the file (just one integer)
x = datetime.datetime.now().strftime(â%Y-%m-%d %H:%M:%S.%fâ)
s.write(dict(x=x, y=y))
graphdata.close() #close the file
time.sleep(1)
s.close()