【问题标题】:How to create two y-axes streaming plotly如何创建两个 y 轴流式传输
【发布时间】:2015-03-25 12:32:32
【问题描述】:

我按照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:

我修复了代码,不再抛出错误。但是仍然没有数据被绘制到图表上。我得到的只是轴:

【问题讨论】:

  • 当然,您应该在第二个查询中将“y2”作为您的轴(或者您可能需要将它们组合起来{'x': datetime.datetime.now(), 'y':s.temperature(),'y2':s.humidity()}
  • @JoranBeasley 我已经尝试了这两个选项并且都返回Invalid key, 'y2', for class, 'Scatter'.
  • 你真的在循环吗?运行终端时,您是否在终端中看到调试打印?
  • @JoranBeasley 是的,循环正在工作
  • 天哪,我相信问题出在 datetime.datetime.now() 上。回家后我会尝试将其更改为 time.time()。

标签: python plotly


【解决方案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()

这是图表的外观:

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-01-21
    • 2018-03-28
    • 1970-01-01
    • 1970-01-01
    • 2019-11-19
    • 1970-01-01
    • 2018-10-07
    相关资源
    最近更新 更多