簡體   English   中英

如何繪制兩個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個流。 您需要單獨的流標記和流來獲取溫度和濕度。

這是一個使用Raspberry Pi的Adafruit Python庫的工作示例。 一個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()

圖表外觀如下:

在此處輸入圖片說明

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM