I have created a figure with a variable number of subplots using matplotlib. I have added a scrollbar to the page containing this figure so that the user would be able to scroll through the various figures. The scrollbar, through many attempts, is now functional, however, the figure seems to cut off where the original bottom of the page is, leaving some subplots cut off (See Image).
The sample code below is supposed to mimic the issue I am running in to. However, the code below seems to have its own issue: it wont stretch the figure as it does in my original code to reproduce the image attached. However, it should still give you an idea - I'd like the graphs to stretch past the regular frame size, and for the scrollbar to allow the user to view all subplots.
import tkinter as tk
from tkinter import *
import matplotlib
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
from matplotlib.figure import Figure
f = Figure(figsize = (1,1), dpi = 100)
a = []
for i in range(0,20):
a.append(f.add_subplot(20,1,i+1))
f.set_figheight(60)
f.set_figwidth(14)
for i in range(0,20):
a[i].plot([1,2,3,4,5],[1,2,3,4,5])
a[i].set_title('Graph ' + str(i+1))
class App(tk.Tk):
def __init__(self, *args, **kwargs):
tk.Tk.__init__(self, *args, **kwargs)
container = tk.Frame(self)
container.pack(side = "top", fill = "both", expand = True)
container.grid_columnconfigure(0,weight=1)
container.grid_rowconfigure(0,weight = 1)
self.minsize(620,400)
self.geometry("{0}x{1}+0+0".format(self.winfo_screenwidth(), self.winfo_screenheight()))
self.update_idletasks()
self.frames = {}
for F in (StartPage, PageTwo):
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)
button_p1 = tk.Button(self, text = "PageTwo →",
command = lambda: controller.show_frame(PageTwo))
button_p1.pack(side = TOP, fill = X)
label = tk.Label(self, text = "Start Page")
label.pack(fill = X, expand = True)
class PageTwo(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
button_1 = tk.Button(self, text = "← Start Page",
command = lambda: controller.show_frame(StartPage))
button_1.pack(side = LEFT, anchor = N)
label_p1 = tk.Label(self, text = "PageTwo")
label_p1.pack()
canvas = FigureCanvasTkAgg(f,self)
canvas.draw()
canvas.get_tk_widget().pack(side=tk.BOTTOM,fill=tk.X,expand = True)
scroll=Scrollbar(canvas.get_tk_widget())
scroll.config(command=canvas.get_tk_widget().yview)
scroll.pack(side = RIGHT, fill=Y)
canvas.get_tk_widget().pack(side = LEFT, expand = YES, fill = BOTH)
canvas.get_tk_widget().config(yscrollcommand=scroll.set)
canvas.get_tk_widget().config(scrollregion=(0,0,1000,10000),confine=True)
app = App()
app.mainloop()
I've been searching for a solution for the past few days, but have not found one specifically tailored to my problem or one that I could change to fit.
I assume that the solution should entail increasing the frame height or something similar - I've attempted to do this with no luck.
I am hoping to keep the same window size for all other pages - I only want to increase the height of PageTwo for the scrollbar purpose.
This solution may not solve the issue of scrolling through all the axes but I found a bug that could be leading to the subplots being cut off too soon. The change I'd make is in the instantiation of the Scrollbar object. Passing the canvas object rather than the widget resolved the same issue I had.
class PageTwo(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
button_1 = tk.Button(self, text = "← Start Page",
command = lambda: controller.show_frame(StartPage))
button_1.pack(side = LEFT, anchor = N)
label_p1 = tk.Label(self, text = "PageTwo")
label_p1.pack()
canvas = FigureCanvasTkAgg(f,self)
canvas.draw()
canvas.get_tk_widget().pack(side=tk.BOTTOM,fill=tk.X,expand = True)
scroll=Scrollbar(canvas)
scroll.config(command=canvas.get_tk_widget().yview)
scroll.pack(side = RIGHT, fill=Y)
canvas.get_tk_widget().pack(side = LEFT, expand = YES, fill = BOTH)
canvas.get_tk_widget().config(yscrollcommand=scroll.set)
canvas.get_tk_widget().config(scrollregion=(0,0,1000,10000),confine=True)