8
13

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 5 years have passed since last update.

tkinterにDataFrameのグラフを表示する

Posted at

卒研が大変なito.ur.rightです。
睡眠に関するものは実験が大変です。

筋電とか心電に睡眠状態のラベルづけを行いたいので、それ用にプログラムを書くことに。
Tkinterとpandasを用いて、DataFrameのグラフを見ながらGUIを用いてラベル付けできるwindowを表示したい。
しかし、Tkinterで調べるとmatplotlibとの組み合わせしか出でこず、DataFrameはどうしたらいいのかわからない。
色々見ていると、Tkinterのcanvasに渡すFigureのsubplotをplot関数に引数として渡すとできる気がする。

test.py

import matplotlib
matplotlib.use('TkAgg')

from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
from matplotlib.figure import Figure
import tkinter
import pandas as pd

def readyCsv(name):
    df = pd.read_csv('data/'+name+'.csv',encoding="Shift-JIS",low_memory=False)
    df = df.fillna(0)
    df = df[2:].astype(float)
    return df

def makeFigure(df,root):
    #Tkinterに表示させるFigureを生成
    fig = Figure(figsize=(20, 6), dpi=100)

    #Figureにsubplotを追加
    ax1 = fig.add_subplot(3,1,1)
    ax2 = fig.add_subplot(3,1,2)
    ax3 = fig.add_subplot(3,1,3)
    
    #気にしないゾーン
    start = (count*10000)
    end = ((count+1)*10000)
    df = df[start:end]

        #plot関数の引数に"ax=ax1"を入れる
    #すると、figのax1にdfが表示される
    df.plot(ax=ax1,  **cargs)


    #tkinterに追加するcanvasにfigを追加する
    canvas = FigureCanvasTkAgg(fig, master=root)
    canvas.get_tk_widget().pack(side=tkinter.BOTTOM, expand=0)
    canvas._tkcanvas.pack(side=tkinter.BOTTOM, expand=0)
    canvas.show()

if __name__ == '__main__':
    #CSVを読み込んでます
    df = readyCsv(name)
    #plotの設定
    cargs = {'linewidth':'0.5','kind':'line', 'use_index':True}
    
    for count in range(0, int(df.shape[0]/10000)-1):
        root = tkinter.Tk()#tkinter生成
        root.title(str(int(count*20/60))+""+str(count*20%60)+"")
        makeFigure(df,root)


        root.mainloop()

やったらできました。

参考ページ

Tkinterでグラフを描画して、ボタンをクリックする度にグラフを更新する

8
13
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
8
13

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?