Python: How do I make a Matplotlib Figure with Subplots expand to fill the entire FigureCanvasTkAgg its on?

Viewed 168

I'm making a Tkinter GUI and I'm trying to get visuals for it. I want to plot some seaborn graphs on a scrolling canvas so that the graphs can be sized properly. I make a figure and an axes list using matplotlib.pyplot.subplots and then put it onto a FigureCanvasTkAgg which I attach two scrollbars to. I can scroll all over the canvas but the figure remains in the a small part of the canvas.

How can I get the figure to take up the entire area of the canvas and then scroll to the different parts?

Basically I want the output achieved here: Scrollbar on Matplotlib showing page

but instead of PyQt using Tkinter.

import sys

import tkinter as tk
from tkinter import ttk

import matplotlib
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, NavigationToolbar2Tk
from matplotlib.figure import Figure
import matplotlib.pyplot as plt
matplotlib.use("TkAgg")

from matplotlib import style
style.use("ggplot")


plt.rcParams.update({'font.size': 12})


root = tk.Tk()

# frame = ttk.Frame(root)
# frame.pack(expand = True)



combMapFig, combMapAxes = plt.subplots(nrows = 5, ncols = 10, figsize = (100,100), dpi = 100)
    # axes_list = axes.flatten()
    # axes_list = axes_list.tolist()
combMapAxes = combMapAxes.tolist()


# plt.gca().set_position([0,0,1,1])


mapCanvas = FigureCanvasTkAgg(combMapFig, root)
mapCanvas.draw()



mapXScroll = tk.Scrollbar(root, orient = 'horizontal')
mapXScroll.config(command = mapCanvas.get_tk_widget().xview)
mapXScroll.pack(side = tk.BOTTOM, fill = tk.X, expand = tk.FALSE)
  
mapYScroll = tk.Scrollbar(root)
mapYScroll.config(command = mapCanvas.get_tk_widget().yview)
mapYScroll.pack(side = tk.RIGHT, fill = tk.Y, expand = tk.FALSE)
  
mapCanvas.get_tk_widget().config(yscrollcommand = mapYScroll.set)
mapCanvas.get_tk_widget().config(xscrollcommand = mapXScroll.set)
  
#             mapCanvas.get_tk_widget().config(scrollregion = mapCanvas.get_tk_widget().bbox("all"))
mapCanvas.get_tk_widget().config(width = 500, height = 500)
mapCanvas.get_tk_widget().config(scrollregion = (0,0, 5000,5000))


mapCanvas.get_tk_widget().pack(side = tk.LEFT, expand = True, fill = tk.BOTH)
mapCanvas._tkcanvas.pack(side = tk.LEFT, expand = True, fill = tk.BOTH)





root.rowconfigure(0, weight = 1)
root.columnconfigure(0, weight = 1)


try:
    root.mainloop()
except KeyboardInterrupt as e:
    sys.exit("Keyboard Interrupt. Program Closed")

Current Output

Blank Canvas where the figure should expand to

Thanks

0 Answers
Related