I want to build a GUI to view matplotlib figures with forward and backward buttons. GUI is just similar to view images. To an extent, I could get the results I need. The problem is: that when I use the backward button(<<) 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()
I want to build a GUI to view matplotlib figures with forward and backward buttons. GUI is just similar to view images. To an extent, I could get the results I need. The problem is: that when I use the backward button(<<) 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()
Share
Improve this question
edited Feb 5 at 7:50
pranjal
asked Jan 31 at 10:30
pranjalpranjal
11 bronze badge
1 Answer
Reset to default 0Got the solution to the problem in the code. Still, I don't understand what is the logic behind it. Following are the modifications to the codes: (Thanks in advance for any further modification or comment )
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 in 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()
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745268087a4619580.html
评论列表(0条)