2017-07-26 15 views
1

プロンプトが表示されたら複数のプロットをプロットするtkinterを使用してGUIを作成しようとしています。今すぐ私のコードが動作し、それは1つの図にグラフのすべてをプロットします。私は同じGUI内の別々の図にグラフをプロットしたいと思います。複数のグラフを同じウィンドウ内の別々のtkinter図形にプロットする

これは、同じTkinterの図にグラフのすべてをプロット私のコードです:

import myModule 
from myModule import * 
import SA_apis 
from SA_apis import * 
import matplotlib 
matplotlib.use("TkAgg") 
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, NavigationToolbar2TkAgg 
from matplotlib.figure import Figure 
import tkinter as tk 
from tkinter import ttk 
from matplotlib import pyplot as plt 

LARGE_FONT= ("Verdana", 12) 
NORM_FONT= ("Verdana", 10) 
SMALL_FONT= ("Verdana", 8) 
plt.style.use("dark_background") 
plt.style.use("seaborn-bright") 

def popupmsg(msg): 
    popup = tk.Tk() 
    popup.wm_title("Menu") 
    label = ttk.Label(popup, text=msg, font=NORM_FONT) 
    label.pack(side="top", fill="x", pady=10) 
    B1 = ttk.Button(popup, text="Okay", command = popup.destroy) 
    B1.pack() 
    popup.mainloop() 


class TkGraph(tk.Tk): 
    def __init__(self, *args, **kwargs): 
     tk.Tk.__init__(self, *args, **kwargs) 
     tk.Tk.wm_title(self, "TkGraph") 

     container = tk.Frame(self) 
     container.pack(side="top", fill="both", expand = True) 
     container.grid_rowconfigure(0, weight=1) 
     container.grid_columnconfigure(0, weight=1) 

     menubar = tk.Menu(container) 
     filemenu = tk.Menu(menubar, tearoff=0) 
     filemenu.add_command(label="New", command = lambda: popupmsg("This is not yet supported")) 
     filemenu.add_command(label="Add", command = lambda: popupmsg("This is not yet supported")) 
     filemenu.add_command(label="Exit", command=quit) 
     menubar.add_cascade(label="File", menu=filemenu) 
     tk.Tk.config(self, menu=menubar) 

     parameter = tk.Menu(menubar, tearoff=0) 
     parameter.add_command(label="Selection", command = lambda: popupmsg("This is not yet supported")) 
     menubar.add_cascade(label="Parameter", menu=parameter) 
     tk.Tk.config(self, menu=menubar) 

     timemenu = tk.Menu(menubar, tearoff=0) 
     timemenu.add_command(label="Selection", command = lambda: popupmsg("This is not yet supported")) 
     menubar.add_cascade(label="Time", menu=parameter) 
     tk.Tk.config(self, menu=menubar) 

     helpmenu = tk.Menu(menubar, tearoff=0) 
     helpmenu.add_command(label="Selection", command = lambda: popupmsg("This is not yet supported")) 
     menubar.add_cascade(label="Help", menu=parameter) 
     tk.Tk.config(self, menu=menubar) 

     self.frames = {} 
     for F in (StartPage, PageOne): 
      frame = F(container, self) 
      self.frames[F] = frame 
      frame.grid(row=0, column=0, sticky="nsew") 
     self.show_frame(StartPage) 
    def show_frame(self, cont): 
     frame = self.frames[cont] 
     frame.tkraise() 

class StartPage(tk.Frame): 
    def __init__(self, parent, controller): 
     tk.Frame.__init__(self,parent) 
     label = tk.Label(self, text="Home", font=LARGE_FONT) 
     label.pack(pady=10,padx=10) 
     button1 = ttk.Button(self, text="Graph", 
     command=lambda: controller.show_frame(PageOne)) 
     button1.pack() 


class PageOne(tk.Frame): 
    def __init__(self, parent, controller): 
     tk.Frame.__init__(self, parent) 
     label = tk.Label(self, text="Graph", font=LARGE_FONT) 
     label.pack(pady=10,padx=10) 
     button1 = ttk.Button(self, text="Back to Home", 
         command=lambda: controller.show_frame(StartPage)) 
     button1.pack() 

    """This is the code for the plotted graph, for each parameter you wish to plot you need to have a seperate x, y coordinate system. 
    You got the amount of points for each parameter on SA_apis now you just paste that number in the code.""" 

    #Graph Code 
    x = (g[0].time[:111673]) 
    y = (g[0].data.f[:111673]) 
    x2 = (h[0].time[:142642]) 
    y2 = (h[0].data.f[:142642]) 
    x3 = (j[0].time[:134970]) 
    y3 = j[0].data.f[:134970] 
    x4 = (p[0].time[:147542]) 
    y4 = (p[0].data.f[:147542]) 
    plt.subplot() 
    fig = plt.figure() 

    """For each parameters x,y coordinates you will need a seperate plt.plot()""" 
    plt.plot(x, y) 
    plt.plot(x2, y2) 
    plt.plot(x3, y3) 
    plt.plot(x4, y4) 
    #descr = (g[0].descr) 
    plt.title('(descr)', fontsize = 15) 
    plt.xlabel('Time', fontsize=12) 
    plt.ylabel('Data', fontsize=12) 
    plt.grid(linestyle = 'dashed') 

    canvas = FigureCanvasTkAgg(fig, self) 
    canvas.show() 
    canvas.get_tk_widget().pack(side=tk.BOTTOM, fill=tk.BOTH, expand=True) 

    toolbar = NavigationToolbar2TkAgg(canvas, self) 
    toolbar.update() 
    canvas._tkcanvas.pack(side=tk.TOP, fill=tk.BOTH, expand=True) 

app = TkGraph() 
app.mainloop() 

enter image description here 私はこのようなグラフ分離を試みた:

x = (g[0].time[:111673]) 
    y = (g[0].data.f[:111673]) 
    plt.plot(x, y) 
    plt.title('(descr)', fontsize = 15) 
    plt.xlabel('Time', fontsize=12) 
    plt.ylabel('Data', fontsize=12) 
    plt.grid(linestyle = 'dashed') 
    fig = plt.figure() 

    x2 = (h[0].time[:142642]) 
    y2 = (h[0].data.f[:142642]) 
    plt.plot(x2, y2) 
    plt.title('(descr)', fontsize = 15) 
    plt.xlabel('Time', fontsize=12) 
    plt.ylabel('Data', fontsize=12) 
    plt.grid(linestyle = 'dashed') 
    fig = plt.figure() 

    x3 = (j[0].time[:134970]) 
    y3 = (j[0].data.f[:134970]) 
    plt.plot(x3, y3) 
    plt.title('(descr)', fontsize = 15) 
    plt.xlabel('Time', fontsize=12) 
    plt.ylabel('Data', fontsize=12) 
    plt.grid(linestyle = 'dashed') 
    fig = plt.figure() 

    x4 = (p[0].time[:147542]) 
    y4 = (p[0].data.f[:147542]) 
    plt.plot(x4, y4) 
    plt.title('(descr)', fontsize = 15) 
    plt.xlabel('Time', fontsize=12) 
    plt.ylabel('Data', fontsize=12) 
    plt.grid(linestyle = 'dashed') 
    fig = plt.figure() 

をしかし、これは面倒であり、すべての4つのグラフが同じtkinterウィンドウにプロットされるわけではありません。これによりtkinterが空になります。

私の質問は次のとおりです:同じtkinterウィンドウに複数のグラフをプロットするにはどうすればよいですか?理想的には、彼らはお互いの隣にプロットされ、私は別のウィンドウを作成したくありません。比較のために同時にすべてのグラフを表示する必要があります。

答えて

-1

サブプロットメソッドでパラメータを設定する必要があります。

plt.subplot(421) 
plt.xlabel('Time in seconds') 
plt.ylabel('Amplitude') 
plt.title('%s HeartBeat ..' % beat_file1) 
plt.plot(timeArray1, Y_filtered1) 

plt.subplot(422) 
plt.xlabel('Time in seconds') 
plt.ylabel('Amplitude') 
plt.title('%s HeartBeat ..' % beat_file2) 
plt.plot(timeArray2, Y_filtered2) 

例はここにあります:https://matplotlib.org/users/pyplot_tutorial.html

+0

私はあなたの答えによって混乱しています。 '421、' 422 'Time in seconds'、 'Amplitude'、 'HeartBeat'とは何ですか?それらは私のコードとどう関係していますか?私の回答に投稿したコードに関する十分な情報を私に提供していない。あなたはnumpyを使ったコードにリンクしています。私はpandasデータフレームを使っています。 –

+0

これは単なる私が作ったプログラムです。私がそれをしたとき、同じウィンドウに4つのグラフをプロットしたいので、plt.subplotメソッドでそれを行うことができます。正方形の例を考えてみましょう。 4つのグラフを表示したい場合は、最初のグラフは次のようになります。 'plt.subplot(221)'これは最初のグラフを左隅に置く方法です。等... –

+0

リンクに例があります。見てください:)他の行は見えません。subplot()メソッドを見てください。 –

0

は、あなたは、単にFrameウィジェットを使用する必要があります。

複数のフレームを同じウィンドウに表示することができます。そのため、各グラフに必要な各フレームを作成し、そのように動作させることができます。

すでにフレームを使用していることを確認して、セットアップするのは難しくありません。

関連する問題