如何绘制地创建两个 y 轴流
How to create two y-axes streaming plotly
我按照 plotly 示例使用我的 DHT22 传感器成功创建了流式温度图。传感器还提供我也想绘制的湿度。
有可能吗?以下代码是我正在尝试的代码,但 抛出异常:plotly.exceptions.PlotlyAccountError: Uh oh, an error occured on the server.
没有数据被绘制到图表中(见下文)。
with open('./plotly.conf') as config_file:
plotly_user_config = json.load(config_file)
py.sign_in(plotly_user_config["plotly_username"], plotly_user_config["plotly_api_key"])
streamObj = Stream(token=plotly_user_config['plotly_streaming_tokens'][0], maxpoints=4032)
trace1 = Scatter(x=[],y=[],stream=streamObj,name='Temperature')
trace2 = Scatter(x=[],y=[],yaxis='y2',stream=streamObj,name='Humidity')
data = Data([trace1,trace2])
layout = Layout(
title='Temperature and Humidity from DHT22 on RaspberryPI',
yaxis=YAxis(
title='Celcius'),
yaxis2=YAxis(
title='%',
titlefont=Font(color='rgb(148, 103, 189)'),
tickfont=Font(color='rgb(148, 103, 189)'),
overlaying='y',
side='right'))
fig = Figure(data=data, layout=layout)
url = py.plot(fig, filename='raspberry-temp-humi-stream')
dataStream = py.Stream(plotly_user_config['plotly_streaming_tokens'][0])
dataStream.open()
#MY SENSOR READING LOOP HERE
dataStream.write({'x': datetime.datetime.now(), 'y':s.temperature()})
dataStream.write({'x': datetime.datetime.now(), 'y':s.humidity()})
#END OF MY LOOP
更新 1:
我修复了代码,错误不再出现。但仍然没有数据被绘制到图表中。我得到的只是轴:
我认为问题在于您对两个读数都使用了 1 个流。您需要单独的流令牌和温度和湿度流。
这是一个使用 Adafruit Python 库的工作示例 Raspberry Pi。 AM2302 传感器连接到我的 Raspberry Pi:
上的引脚 17
#!/usr/bin/python
import subprocess
import re
import sys
import time
import datetime
import plotly.plotly as py # plotly library
from plotly.graph_objs import Scatter, Layout, Figure, Data, Stream, YAxis
# Plot.ly credentials and stream tokens
username = 'plotly_username'
api_key = 'plotly_api_key'
stream_token_temperature = 'stream_token_1'
stream_token_humidity = 'stream_token_2'
py.sign_in(username, api_key)
trace_temperature = Scatter(
x=[],
y=[],
stream=Stream(
token=stream_token_temperature
),
yaxis='y'
)
trace_humidity = Scatter(
x=[],
y=[],
stream=Stream(
token=stream_token_humidity
),
yaxis='y2'
)
layout = Layout(
title='Raspberry Pi - Temperature and humidity',
yaxis=YAxis(
title='Celcius'
),
yaxis2=YAxis(
title='%',
side='right',
overlaying="y"
)
)
data = Data([trace_temperature, trace_humidity])
fig = Figure(data=data, layout=layout)
print py.plot(fig, filename='Raspberry Pi - Temperature and humidity')
stream_temperature = py.Stream(stream_token_temperature)
stream_temperature.open()
stream_humidity = py.Stream(stream_token_humidity)
stream_humidity.open()
while(True):
# Run the DHT program to get the humidity and temperature readings!
output = subprocess.check_output(["./Adafruit_DHT", "2302", "17"]);
print output
# search for temperature printout
matches = re.search("Temp =\s+([0-9.]+)", output)
if (not matches):
time.sleep(3)
continue
temp = float(matches.group(1))
# search for humidity printout
matches = re.search("Hum =\s+([0-9.]+)", output)
if (not matches):
time.sleep(3)
continue
humidity = float(matches.group(1))
print "Temperature: %.1f C" % temp
print "Humidity: %.1f %%" % humidity
# Append the data to the streams, including a timestamp
now = datetime.datetime.now()
stream_temperature.write({'x': now, 'y': temp })
stream_humidity.write({'x': now, 'y': humidity })
# Wait 30 seconds before continuing
time.sleep(30)
stream_temperature.close()
stream_humidity.close()
图表是这样的:
我按照 plotly 示例使用我的 DHT22 传感器成功创建了流式温度图。传感器还提供我也想绘制的湿度。
有可能吗?以下代码是我正在尝试的代码,但 抛出异常: 没有数据被绘制到图表中(见下文)。plotly.exceptions.PlotlyAccountError: Uh oh, an error occured on the server.
with open('./plotly.conf') as config_file:
plotly_user_config = json.load(config_file)
py.sign_in(plotly_user_config["plotly_username"], plotly_user_config["plotly_api_key"])
streamObj = Stream(token=plotly_user_config['plotly_streaming_tokens'][0], maxpoints=4032)
trace1 = Scatter(x=[],y=[],stream=streamObj,name='Temperature')
trace2 = Scatter(x=[],y=[],yaxis='y2',stream=streamObj,name='Humidity')
data = Data([trace1,trace2])
layout = Layout(
title='Temperature and Humidity from DHT22 on RaspberryPI',
yaxis=YAxis(
title='Celcius'),
yaxis2=YAxis(
title='%',
titlefont=Font(color='rgb(148, 103, 189)'),
tickfont=Font(color='rgb(148, 103, 189)'),
overlaying='y',
side='right'))
fig = Figure(data=data, layout=layout)
url = py.plot(fig, filename='raspberry-temp-humi-stream')
dataStream = py.Stream(plotly_user_config['plotly_streaming_tokens'][0])
dataStream.open()
#MY SENSOR READING LOOP HERE
dataStream.write({'x': datetime.datetime.now(), 'y':s.temperature()})
dataStream.write({'x': datetime.datetime.now(), 'y':s.humidity()})
#END OF MY LOOP
更新 1:
我修复了代码,错误不再出现。但仍然没有数据被绘制到图表中。我得到的只是轴:
我认为问题在于您对两个读数都使用了 1 个流。您需要单独的流令牌和温度和湿度流。
这是一个使用 Adafruit Python 库的工作示例 Raspberry Pi。 AM2302 传感器连接到我的 Raspberry Pi:
上的引脚 17#!/usr/bin/python
import subprocess
import re
import sys
import time
import datetime
import plotly.plotly as py # plotly library
from plotly.graph_objs import Scatter, Layout, Figure, Data, Stream, YAxis
# Plot.ly credentials and stream tokens
username = 'plotly_username'
api_key = 'plotly_api_key'
stream_token_temperature = 'stream_token_1'
stream_token_humidity = 'stream_token_2'
py.sign_in(username, api_key)
trace_temperature = Scatter(
x=[],
y=[],
stream=Stream(
token=stream_token_temperature
),
yaxis='y'
)
trace_humidity = Scatter(
x=[],
y=[],
stream=Stream(
token=stream_token_humidity
),
yaxis='y2'
)
layout = Layout(
title='Raspberry Pi - Temperature and humidity',
yaxis=YAxis(
title='Celcius'
),
yaxis2=YAxis(
title='%',
side='right',
overlaying="y"
)
)
data = Data([trace_temperature, trace_humidity])
fig = Figure(data=data, layout=layout)
print py.plot(fig, filename='Raspberry Pi - Temperature and humidity')
stream_temperature = py.Stream(stream_token_temperature)
stream_temperature.open()
stream_humidity = py.Stream(stream_token_humidity)
stream_humidity.open()
while(True):
# Run the DHT program to get the humidity and temperature readings!
output = subprocess.check_output(["./Adafruit_DHT", "2302", "17"]);
print output
# search for temperature printout
matches = re.search("Temp =\s+([0-9.]+)", output)
if (not matches):
time.sleep(3)
continue
temp = float(matches.group(1))
# search for humidity printout
matches = re.search("Hum =\s+([0-9.]+)", output)
if (not matches):
time.sleep(3)
continue
humidity = float(matches.group(1))
print "Temperature: %.1f C" % temp
print "Humidity: %.1f %%" % humidity
# Append the data to the streams, including a timestamp
now = datetime.datetime.now()
stream_temperature.write({'x': now, 'y': temp })
stream_humidity.write({'x': now, 'y': humidity })
# Wait 30 seconds before continuing
time.sleep(30)
stream_temperature.close()
stream_humidity.close()
图表是这样的: