如何创建两个y轴流图

时间:2015-01-27 00:21:26

标签: python 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:

我修复了代码并且不再抛出错误。但仍然没有数据绘制到图表。我得到的只是轴: only axis graph

1 个答案:

答案 0 :(得分:4)

我认为问题在于您为两个读数使用了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()

图表的外观如下:

enter image description here