2017-07-26 46 views
1

我試圖用tkinter創建一個GUI,當出現提示時會繪製多個圖。現在我的代碼工作,它將所有圖形繪製到一個圖上。我想用同一個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() 

但這是乏味的,而不會導致所有四個圖形都被繪製在相同的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''秒','振幅'和'HeartBeat',它們與我的代碼有什麼關係?您沒有向我提供關於您發佈到我的答案的代碼的足夠信息。你鏈接到一個代碼,使用numpy我已經使用熊貓數據框。 –

+0

這只是我做的一個程序。當我這樣做時,我想在同一個窗口上繪製四個圖形,所以可以使用plt.subplot方法。以一個正方形的例子。如果你想顯示4個圖,第一個圖有這個:'plt.subplot(221)'這是如何把第一個圖放在左上角。 etc .. –

+0

你有一個例子在鏈接..只是看看:)看不到其他線只是看subplot()方法 –

0

您只需使用Frame部件。

您可以在同一個窗口中有多個框架,因此您可以爲每個圖創建每個框架,並以這種方式處理它們。

看到您已經在使用框架,您應該不難設置。

相關問題