How to create two y-axes streaming plotly

 ̄綄美尐妖づ 提交于 2019-12-12 09:26:35

问题


I followed the plotly examples to successfully create a streaming temperature graph using my DHT22 sensor. The sensor also provides humidity which I would like to plot as well.

Is it possible somehow? The following code is what I'm trying but an exception is thrown: plotly.exceptions.PlotlyAccountError: Uh oh, an error occured on the server. no data is being plot to the graph (see bellow).

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

Update 1:

I fixed the code and the error is not thrown anymore. But still no data is plot to the graph. All I get are the axis:


回答1:


I think the problem is that you're using 1 stream for both readings. You need separate stream tokens and streams for the temperature and the humidity.

Here's a working example using the Adafruit Python library for the Raspberry Pi. An AM2302 sensor is connected to pin 17 on my Raspberry Pi:

#!/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()

This is how the chart looks:



来源:https://stackoverflow.com/questions/28161244/how-to-create-two-y-axes-streaming-plotly

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!