我正在使用运行 Python 3 的 Raspberry Pi 记录来自 5 个温度传感器的数据。
一切运行良好,我现在想在一张图上显示 5 个温度的图,每 10 分钟左右更新一次。我想使用 Plotly。
我编写了以下代码来测试这个想法。
#many_lines2
# tryimg to sort out why x is sent more than once when using extend
import time
import plotly.plotly as py
from plotly.graph_objs import *
import plotly.tools as tls
#tls.set_credentials_file(username=, api_key)
from datetime import datetime
for count in range (1,5):
x1 = count
y1 = count * 2
y2 = count * 3
y3 = count * 4
trace1 = Scatter(x=x1,y = y1,mode = "lines")
trace2 = Scatter(x=x1,y = y2,mode = "lines")
trace3 = Scatter(x=x1,y = y3,mode = "lines")
data = [trace1,trace2,trace3]
py.plot (data,filename="3lines6", fileopt = "extend")
time.sleep(60)
在此处查看 plotly 接收的绘图和数据https://plot.ly/~steverat/334/trace0-trace1-trace2/ 查看 plotly 接收的数据的数据选项卡。
在我看来,好像数据表中的 x 值在发送第一个值后被添加了三次。
我通过在 python 中使用 .append 创建值列表来获得正确的结果。这会导致列表很长,更多数据要发送到 plotly 并且似乎是错误的。
执行此操作的代码如下,可以在此处找到有关 plotly 服务的数据。https://plot.ly/~steverat/270
# using lists and append to send data to plotly
import time
import plotly.plotly as py
from plotly.graph_objs import *
import plotly.tools as tls
#tls.set_credentials_file(username='steverat', api_key='f0qs8y2vj8')
from datetime import datetime
xlist = []
y1list= []
y2list = []
y3list = []
for count in range (1,5):
xlist.append (count)
y1list.append (count * 2)
y2list.append (count * 3)
y3list.append (count * 4)
print "xlist = ", xlist
print "y1list = ", y1list
print "y2list = ", y2list
trace1 = Scatter(x=xlist,y = y1list,mode = "lines")
trace2 = Scatter(x=xlist,y = y2list,mode = "lines")
trace3 = Scatter(x=xlist,y = y3list,mode = "lines")
data = [trace1,trace2,trace3]
py.plot (data,filename="3lines2")
time.sleep(60)
我在网上搜索过,可以找到流式传输数据的示例,但我只想每 10 次更新一次图表。
我错过了什么明显的东西吗???
干杯
史蒂夫