Black Lives Matter. Please consider donating to Black Girls Code today.
Learn how to use COVID-19 data in open source Dash apps. Register for the Sept 23rd webinar with IQT!

Plotly graph streaming, getting data from local text file creates weird behavior

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. :

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”:
"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 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(

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)


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 =’%Y-%m-%d %H:%M:%S.%f’)
s.write(dict(x=x, y=y))
graphdata.close() #close the file