5

I'm plotting temperature and humidity readings from raspberry pi to plotly.

On the same y axis they appear fine.

When I add a second y axis via layout, the temperature trace (trace1) doesn't show.

On plotly itself it shows the data for both trace1 and trace2 but it's not plotting trace1 for some reason.

Any ideas?!

import plotly.plotly as py
import json
import time
import datetime as dt
import plotly.graph_objs as go
import numpy as np
import sqlite3
import pandas as pd

con = sqlite3.connect('/home/pi/environment.db')
#c = conn.cursor()
df = pd.read_sql_query("SELECT date_time, temp, humid FROM readings", 
con)

df['temp_MA'] = df.temp.rolling(10).mean()
df['humid_MA'] = df.humid.rolling(10).mean()

trace1 = go.Scatter(
        name = 'Temperature degC',
        x=df.date_time,
        y=df.temp_MA,
        yaxis = 'y1'
        )
trace2 = go.Scatter(
        name = 'Rel Humidity %',
        x=df.date_time,
        y=df.humid_MA,
        yaxis = 'y2'
        )
data = [trace1, trace2]
layout = go.Layout(
        yaxis=dict(
            title='Temperature deg C',
            side='left'
            ),
        yaxis2=dict(
            title='Humidity Rel %',
            side='right'
            )
        )
fig = go.Figure(data=data, layout=layout)
py.plot(fig)

con.close()

The plotly graph is here: https://plot.ly/~uilfut/58/temperature-degc-vs-rel-humidity/

justdata
  • 153
  • 1
  • 7

1 Answers1

10

Just in case anyone else discovers this thread from googling - I figured out the missing line of code...

Under layout, yaxis2, you have to specify overlaying='y'.

It works now :)

layout = go.Layout(
        yaxis=dict(
            title='Temperature deg C',
            side='left'
            ),
        yaxis2=dict(
            title='Humidity Rel %',
            side='right',
            overlaying='y'
            )
        )
justdata
  • 153
  • 1
  • 7
  • If you want `y` to overlay `y2` you simply set `overlaying='y2'` in `yaxis` dict instead :) – mkst Jul 03 '20 at 10:12