gui用前向和向后按钮查看matplotlib图

问题描述 投票:0回答:1
我想构建一个GUI以使用前向和向后按钮查看matplotlib数字。 GUI与查看图像相似。 在某种程度上,我可以得到所需的结果。 问题是:当我使用向后按钮时(

<<) the size of the figure increases and I could not find out why. Is there anyone out there who can help understand what is missing and fix the size so it does not increase? Following are the codes for reference:

from tkinter import * import tkinter as tk from matplotlib.backends.backend_tkagg import (FigureCanvasTkAgg,NavigationToolbar2Tk) import matplotlib.pyplot as plt # Variables for line plots x= [1,2,3,4] y=[4,5,6,7] x1=[5,6,7,3,1] y1=[8,9,10,2,4] x2=[1,4,2,5,9,3] y2=[2,5,1,6,1,4] # Creating figures fig1,ax1 = plt.subplots() ax1.plot(x, y, marker='o', label="Data Points",color='red') ax1.set_title("Basic Components of Matplotlib Figure") ax1.set_xlabel("X-Axis") ax1.set_ylabel("Y-Axis") fig1.tight_layout() # plt.show() fig2,ax2 = plt.subplots() ax2.plot(x1, y1, marker='o', label="Data Points",color='green') ax2.set_title("Basic Components of Matplotlib Figure") ax2.set_xlabel("X-Axis") ax2.set_ylabel("Y-Axis") fig2.tight_layout() fig3,ax3 = plt.subplots() ax3.plot(x2, y2, marker='o', label="Data Points",color='orange') ax3.set_title("Basic Components of Matplotlib Figure") ax3.set_xlabel("X-Axis") ax3.set_ylabel("Y-Axis") fig3.tight_layout() fig_list = [fig1,fig2,fig3] def frwrd(number): global Fw_Btn global Bck_Btn global exit_btn global frame1 global Fig_Canvas frame1.destroy() # Creating a frame in root frame1= Frame(root,bg= "green") frame1.grid(row=1,column=0,columnspan=3,padx=10,pady=10,sticky='nsew') # Create a figure canvas again Fig_Canvas = FigureCanvasTkAgg(fig_list[number-1],master = frame1) Fig_Canvas.draw() Fig_Canvas.get_tk_widget().grid(row=0,column=0) # Creating buttons again Fw_Btn = Button(root, text = '>>',command=lambda:frwrd(number + 1)) Bck_Btn = Button(root, text = '<<',command= lambda: back(number - 1)) if number ==3: Fw_Btn = Button(root, text = '>>',state=DISABLED) Fw_Btn.grid(row=0,column=2,sticky='nswe') Bck_Btn.grid(row=0,column=0,sticky='nswe') exit_btn.grid(row=0,column=1,sticky='nswe') def back(number): global Fw_Btn global Bck_Btn global exit_btn global frame1 global Fig_Canvas frame1.destroy() # Creating a frame inplace of root frame1= Frame(root,bg= "green") frame1.grid(row=1,column=0,columnspan=3,padx=10,pady=10,sticky='nsew') # Create a figure canvas again Fig_Canvas = FigureCanvasTkAgg(fig_list[number-1],master = frame1) Fig_Canvas.draw() Fig_Canvas.get_tk_widget().grid(row=0,column=0) # Creating buttons again Fw_Btn = Button(root, text = '>>',command=lambda:frwrd(number + 1)) Bck_Btn = Button(root, text = '<<',command= lambda: back(number - 1)) if number ==1: Bck_Btn = Button(root, text = '<<',state=DISABLED) Fw_Btn.grid(row=0,column=2,sticky='nswe') Bck_Btn.grid(row=0,column=0,sticky='nswe') exit_btn.grid(row=0,column=1,sticky='nswe') # root of tkinter root = Tk() root.geometry('700x600') # Buttons Fw_Btn = Button(root, text = '>>',command=lambda: frwrd(2)) Bck_Btn = Button(root, text = '<<',command= back,state=DISABLED) exit_btn = Button(root,text = 'Exit',command= root.quit) # Placing buttons Bck_Btn.grid(row=0,column=0,sticky='nswe') exit_btn.grid(row=0,column=1,sticky='nswe') Fw_Btn.grid(row=0,column=2,sticky='nswe') # Creating a frame inplace of root frame1= Frame(root,bg= "green") frame1.grid(row=1,column=0,columnspan=3,padx=10,pady=10,sticky='nsew') # Creating a figure canvas Fig_Canvas = FigureCanvasTkAgg(fig1,master = frame1) Fig_Canvas.draw() Fig_Canvas.get_tk_widget().grid(row=0,column=0) # Configureing the root root.grid_columnconfigure([0,1,2],weight=1) root.grid_rowconfigure(1,weight=1) root.mainloop()
    
python matplotlib tkinter plot embedding
1个回答
0
投票
将解决方案的解决方案用于代码中的问题。不过,我不明白它背后的逻辑是什么。以下是对代码的修改: (事先感谢您的进一步修改或评论)

from tkinter import * import tkinter as tk from PIL import ImageTk,Image from matplotlib.backends.backend_tkagg import (FigureCanvasTkAgg,NavigationToolbar2Tk) import matplotlib.pyplot as plt from matplotlib.figure import Figure # Variables for line plots x= [1,2,3,4] y=[4,5,6,7] x1=[5,6,7,3,1] y1=[8,9,10,2,4] x2=[1,4,2,5,9,3] y2=[2,5,1,6,1,4] #------------------------------------- # MODIFICATION TO EARLIER CODES # the figure that will contain the plot fig1 = Figure(figsize = (5, 5),dpi = 100) # list of squares # adding the subplot plot1 = fig1.add_subplot(111) # plotting the graph plot1.plot(x,y) # the figure that will contain the plot fig2 = Figure(figsize = (5, 5),dpi = 100) # list of squares # adding the subplot plot2 = fig2.add_subplot(111) # plotting the graph plot2.plot(x1,y1) # the figure that will contain the plot fig3 = Figure(figsize = (5, 5),dpi = 100) # list of squares # adding the subplot plot3 = fig3.add_subplot(111) # plotting the graph plot3.plot(x2,y2) #------------------------------------- #Earlier part of the code which is replaced by above # # Creating figures # fig1,ax1 = plt.subplots() # ax1.plot(x, y, marker='o', label="Data Points",color='red') # ax1.set_title("Basic Components of Matplotlib Figure") # ax1.set_xlabel("X-Axis") # ax1.set_ylabel("Y-Axis") # fig1.tight_layout() # # plt.show() # fig2,ax2 = plt.subplots() # ax2.plot(x1, y1, marker='o', label="Data Points",color='green') # ax2.set_title("Basic Components of Matplotlib Figure") # ax2.set_xlabel("X-Axis") # ax2.set_ylabel("Y-Axis") # fig2.tight_layout() # fig3,ax3 = plt.subplots() # ax3.plot(x2, y2, marker='o', label="Data Points",color='orange') # ax3.set_title("Basic Components of Matplotlib Figure") # ax3.set_xlabel("X-Axis") # ax3.set_ylabel("Y-Axis") # fig3.tight_layout() #------------------------------------- fig_list = [fig1,fig2,fig3] def frwrd(number): global Fw_Btn global Bck_Btn global exit_btn global frame1 global Fig_Canvas frame1.destroy() # Creating a frame in root frame1= Frame(root,bg= "green") frame1.grid(row=1,column=0,columnspan=3,padx=10,pady=10,sticky='nsew') # Create a figure canvas again Fig_Canvas = FigureCanvasTkAgg(fig_list[number-1],master = frame1) Fig_Canvas.draw() Fig_Canvas.get_tk_widget().grid(row=0,column=0) # Creating buttons again Fw_Btn = Button(root, text = '>>',command=lambda:frwrd(number + 1)) Bck_Btn = Button(root, text = '<<',command= lambda: back(number - 1)) if number ==3: Fw_Btn = Button(root, text = '>>',state=DISABLED) Fw_Btn.grid(row=0,column=2,sticky='nswe') Bck_Btn.grid(row=0,column=0,sticky='nswe') exit_btn.grid(row=0,column=1,sticky='nswe') def back(number): global Fw_Btn global Bck_Btn global exit_btn global frame1 global Fig_Canvas frame1.destroy() # Creating a frame inplace of root frame1= Frame(root,bg= "green") frame1.grid(row=1,column=0,columnspan=3,padx=10,pady=10,sticky='nsew') # Create a figure canvas again Fig_Canvas = FigureCanvasTkAgg(fig_list[number-1],master = frame1) Fig_Canvas.draw() Fig_Canvas.get_tk_widget().grid(row=0,column=0) # Creating buttons again Fw_Btn = Button(root, text = '>>',command=lambda:frwrd(number + 1)) Bck_Btn = Button(root, text = '<<',command= lambda: back(number - 1)) if number ==1: Bck_Btn = Button(root, text = '<<',state=DISABLED) Fw_Btn.grid(row=0,column=2,sticky='nswe') Bck_Btn.grid(row=0,column=0,sticky='nswe') exit_btn.grid(row=0,column=1,sticky='nswe') # root of tkinter root = Tk() root.geometry('700x600') # Buttons Fw_Btn = Button(root, text = '>>',command=lambda: frwrd(2)) Bck_Btn = Button(root, text = '<<',command= back,state=DISABLED) exit_btn = Button(root,text = 'Exit',command= root.quit) # Placing buttons Bck_Btn.grid(row=0,column=0,sticky='nswe') exit_btn.grid(row=0,column=1,sticky='nswe') Fw_Btn.grid(row=0,column=2,sticky='nswe') # Creating a frame inplace of root frame1= Frame(root,bg= "green") frame1.grid(row=1,column=0,columnspan=3,padx=10,pady=10,sticky='nsew') # Creating a figure canvas Fig_Canvas = FigureCanvasTkAgg(fig1,master = frame1) Fig_Canvas.draw() Fig_Canvas.get_tk_widget().grid(row=0,column=0) # Configureing the root root.grid_columnconfigure([0,1,2],weight=1) root.grid_rowconfigure(1,weight=1) root.mainloop() fig = Figure(figsize=(5, 4), dpi=100) t = np.arange(0, 3, .01) ax = fig.add_subplot() line, = ax.plot(t, 2 * np.sin(2 * np.pi * t)) ax.set_xlabel("time [s]") ax.set_ylabel("f(t)") canvas = FigureCanvasTkAgg(fig, master=root) # A tk.DrawingArea. canvas.draw() x2=[1,4,2,5,9,3] y2=[2,5,1,6,1,4] # the figure that will contain the plot fig1 = Figure(figsize = (5, 5),dpi = 100) # list of squares # adding the subplot plot1 = fig1.add_subplot(111) # plotting the graph plot1.plot(x,y) # the figure that will contain the plot fig2 = Figure(figsize = (5, 5),dpi = 100) # list of squares # adding the subplot plot2 = fig2.add_subplot(111) # plotting the graph plot2.plot(x1,y1) # the figure that will contain the plot fig3 = Figure(figsize = (5, 5),dpi = 100) # list of squares # adding the subplot plot3 = fig3.add_subplot(111) # plotting the graph plot3.plot(x2,y2)
    
最新问题
© www.soinside.com 2019 - 2025. All rights reserved.