plot by python
概要
定期的に採取したセンサーデータをプロットする。
dump辞書のad番地にセンサーデータが格納されている。
- dump [ad]=words
ad番地の名前がnameで、値がval。
- regnames[name]=ad
- regvals[name]=val
dumpからregvalsに値をコピーし、現在時刻と共にhistoryに登録。nowを現在時刻の変数とする。
size=20 history.append([now,regvals.copy()]) if( len(history) > size ) : history.pop(0) x = h[0] for h in history y1 = h[1]["name1"] for h in history y2 = h[1]["name2"] for h in history
x = [hms,hms,hms,...] y1=[a,b,c,...] y2=[a,b,c,...] class history get("x") show() x=his.get("x") y1=his.get("x") y2=his.get("x") fig = fig.plot(x,y1,color=c1) fig.plot(x,y2,color=c2)
全体のイメージ
class Sensor: def __init__(): 初期化 def read(self): self.dump にデータ取り込み now()をタイムスタンプとして登録 def update(self): self.read() self.save() # fileに保存? self.regvals にdumpからデータ転送 self.histにnow=tsとregvalsを追加。 # hist = [[ts,regvals.copy()],...] def gettimes(): histからtimeを取り出す。 def get(self, name): hist からname 取り込み。 def history(self, name): name のhistory 確保
リアルタイム
# -*- coding: utf-8 -*- from matplotlib import pyplot as plt import math import numpy as np import datetime xtime = [0] * 100 y1= [0] * 100 time = 0 sinx = 0 # initialize matplotlib plt.ion() plt.figure() li, = plt.plot(xtime, y1) plt.ylim(-1,1) plt.xlabel("t") plt.ylabel("y") plt.title("continuous plot") while 1 : time += 0.1 sinx = math.sin(time) xtime.append(time) xtime.pop(0) sinxs.append(sinx) sinxs.pop(0) li.set_xdata(xtime) li.set_ydata(y1) plt.xlim(min(xtime), max(xtime)) plt.draw() plt.pause(1.0)
tknter canvas
import matplotlib matplotlib.use('TkAgg') from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg from matplotlib.figure import Figure import tkinter as Tk def Make_graphs(): result = Calc() #具体的な計算 f = result[1] #x軸 sigre = result[2] #y軸1 sigim = result[3] #y軸2 sigabs = result[4] #y軸3 # データをClear re.cla() im.cla() abso.cla() re.plot(f, sigre, "r") im.plot(f, sigim, "b") abso.plot(f, sigabs, "r") canvas.show() if( __name__ = '__main__' ): # 前処理/global variable init root = Tk.Tk() # Canvasを生成 F = Figure(figsize=(10, 3), dpi=100) re = F.add_subplot(131) im = F.add_subplot(132) abso = F.add_subplot(133) canvas = FigureCanvasTkAgg(F, master=root) canvas.get_tk_widget().pack( side = Tk.BOTTOM, expand=0) canvas._tkcanvas.pack( side = Tk.BOTTOM, expand=0) Make_graphs() # グラフ描画 # ボタン作成, 追加 bt = Tk.Button(root, text='UPDATE', command=Make_graphs) bt.pack() root.mainloop()
参考
continuous plot
https://qiita.com/dendensho/items/79e9d2e3d4e8eb5061bc
https://qiita.com/hausen6/items/b1b54f7325745ae43e47
https://qiita.com/shizuma/items/8616bbe3ebe8ab0b6ca1
https://teratail.com/questions/101723
tkinter and DataFrame from pandas
https://qiita.com/ito_ur_right/items/41f183ee96ab2bd7f6e8
clear cla clf close