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!

Is it possible to change axis start-end points at defined ranges for a real-time logging

Is it possible to simultaneously change x-axis start-end points at defined ranges (like sliding windows always presenting the latest captured value) for a real-time temperature logging experiment?

In my graph, the time is on the x-axis and the graph is updated at every 10 min in a loop of Python script logging the room temperature on the y-axis. I want to update x-axis at a defined constant range (for example including data of last 8 hours) of time to present latest data on the graph.

Also, I have tried to update date on my graph’s title but the code didn’t work for me. I have added a line (fig[‘layout’].update() function) to “while loop” section for updating date.

My graph’s link is:
https://plot.ly/~hakan.alyuruk/4/real-time-room-temperature-19-oct-2016/#plot

My Python script is as below:

import sys
import spidev
import time
import plotly.plotly as py
from plotly.graph_objs import *
from plotly.graph_objs import Scatter, Layout, Figure
from time import gmtime, strftime

retrieve date

day_year = strftime("%d %b %Y", time.localtime(time.time()))

draw plotly figure

username = ‘…’ # plotly username
api_key = ‘…’ # plotly api-key
stream_token = ‘…’ # plotly streaming token
py.sign_in(username, api_key)

trace1 = Scatter(
x=[],
y=[],
marker=Marker(
color=‘rgb(7, 55, 99)’,
line=Line(
color=‘rgb(68, 68, 68)’
),
size=8
),
name=‘Temperature (°C)’,
stream=dict(
token=stream_token,
maxpoints=284
)
)

layout = Layout(
showlegend=True,
title=‘Real-Time Room Temperature (’ + day_year + ‘)’,
autosize=True,
font=Font(
color=‘rgb(0, 0, 0)’
),
height=626,
paper_bgcolor=‘rgb(204, 204, 204)’,
plot_bgcolor=‘rgb(217, 217, 217)’,
width=1121,
xaxis=XAxis(
autorange=True,
gridcolor=‘rgb(180, 180, 180)’,
gridwidth=1,
mirror=‘all’,
showgrid=True,
showline=True,
ticks=‘outside’,
title=‘Time’,
type=‘category’
),
yaxis=YAxis(
autorange=True,
gridcolor=‘rgb(180, 180, 180)’,
gridwidth=1,
mirror=‘all’,
showgrid=True,
showline=True,
ticks=‘outside’,
title=‘Temperature (°C)’,
type=‘linear’
)
)

fig = Figure(data=[trace1], layout=layout)
print py.plot(fig, filename=‘Real-Time Room Temperature’)

i = 0
stream = py.Stream(stream_token)
stream.open()

some more codes for logging

sensor reading, logging and plotting loop

while True:
value = readadc(0)
volts = (value * 3.3) / 1024
temperature = volts / (10.0 / 1000)
temp = round(temperature, 1)
day_year = strftime("%d %b %Y", time.localtime(time.time()))
hour_min = strftime("%H:%M", time.localtime(time.time()))
fig[‘layout’].update(title=‘Real-Time Room Temperature (’ + day_year + ‘)’)
stream.write({‘x’: hour_min, ‘y’: temp})
outfile.write(str(day_year)+","+str(hour_min)+","+str(temp)+"\n")
outfile.flush()
i += 1
time.sleep(600.0)

I have found a solution as below:

For defining the ranges of x-axis, I have added nticks and range parameters as given at below lines to the layout section:

layout=Layout(
xaxis=XAxis(
autorange=False,
gridcolor=‘rgb(180, 180, 180)’,
gridwidth=1,
dtick=12,
mirror=‘all’,
showgrid=True,
showline=True,
ticks=‘outside’,
nticks=30,
range=[0,288],
tickangle=-45,
title=‘Time’,
type=‘category’
)
)

To update the graph title, I have revised below code in the loop section at the end:

layout.update(title=‘Real-Time Room Temperature (’ + day_year + ‘)’)

The revision for updating graph title did not work again.

Both lines below do not work in the loop:

fig[‘layout’].update(title=‘Real-Time Room Temperature (’ + day_year + ‘)’)

or

layout.update(title=‘Real-Time Room Temperature (’ + day_year + ‘)’)

I have no problem at writing the day and time with ‘day_year’ and ‘hour_min’ functions to output file.

Any suggestions will be much appreciated.