2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

plotlyで2軸の散布図

Posted at

 前回、ラズパイ4のCPUクロックとCPU温度をゲージを使って描画しました。ここでは、任意の時間ロギングした値を散布図で描画します。
2021-12-08 (3).png

環境

  • ラズパイ4 5.10.63
  • Python 3.9.2
  • plotly 5.4.0

ロギングのプログラム

import subprocess
import time
import datetime

f = open('/home/pi/dat01.txt','w')
for i in range(1,100):
    clk = subprocess.getoutput("vcgencmd measure_clock arm").split('=')
    CLK = int(clk[1])/1000000.0

    temp = subprocess.getoutput("vcgencmd measure_temp").split('=')
    T = float(temp[1].split("'")[0])

    dt_now = datetime.datetime.now()
    writeData = dt_now.isoformat()+","+str(round(CLK,1))+","+str(T)
    print(writeData)
    f.write(writeData+"\n")
    time.sleep(1)
f.close()

 データの一部です。時間、クロック、温度をカンマで区切っています。

2021-12-08T05:08:37.515027,1500.3,43.3
2021-12-08T05:08:38.555517,600.1,42.3
2021-12-08T05:08:39.591100,700.2,42.8
2021-12-08T05:08:40.635839,700.2,42.8

ポイントは2軸表示と個々のスケール

 時間はx、クロックをy1、温度をy2のリストに割り当てています。
 2軸表示はsubplotsを利用します。二つの軸のスケールは自動調整されますが、見やすくありません。
2021-12-07 (5).png
 左側のスケールは、update_layoutのrange = [500,1600]を記述して下限と上限を設定できますが、右側のスケールを制限する方法が見つかりませんでした。右側の軸はsecondary_y=Trueで指定します。
 単独でupdate_yaxesを使うとrangeで下限と上限を設定できることがわかりました。
 横軸の時間は、このplotlyライブラリがうまく処理をしてくれて、わかりやすく表示をしてくれます。

import plotly.graph_objects as go
from plotly.subplots import make_subplots

f = open('dat01.txt','r')
s = f.read().split('\n')
print(len(s))

x=[]
y1=[]
y2=[]
for i in range(1,len(s)-1):
    x.append(s[i].split(',')[0])
    y1.append(float(s[i].split(',')[1]))
    y2.append(float(s[i].split(',')[2]))
print(x)
print(y1)
print(y2)

def scatter(mode, symbol, dash, color, y, name):
    d = go.Scatter(
        mode=mode,
        x=x, y=y,
        texttemplate="(%{x}, %{y})",
        name=name,
        marker=dict(symbol=symbol, color=color),
        line=dict(dash=dash, color=color),
    )
    return d

fig= make_subplots(specs=[[{"secondary_y": True}]])

d1 = scatter(mode="lines+markers", symbol="circle", dash="solid", color="red", y =y1, name="CPU clock")
d2 = scatter(mode="lines+markers", symbol="circle", dash="solid", color="blue", y=y2, name="CPU temp")

fig.add_trace(d1)
fig.add_trace(d2,secondary_y=True)
layout1 = go.Layout(
    yaxis = dict(title="Clock [MHz]", range = [500,1600], dtick=100))

fig.update_layout(layout1)
fig.update_yaxes(title="Temp [`C]", secondary_y=True, range=[30, 80])

fig.show()

2
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?