3

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

Zephyr
  • 11,891
  • 53
  • 45
  • 80
dalbywh
  • 31
  • 1
  • Upon speaking with a colleague, he informed me that with the amount of plots I was trying to plot that Tkinter would run out of memory (and when he ran the above example it did run out of memory). I'm using Anaconda 64 which he said is probably suppressing the memory warning. The app that's going to be using this solution is going to be wrapped into a .exe using Pyinstaller. Will the users regularly experience memory issues or will these memory issues be suppressed? If those memory issues would persist, I'm probably not going to be able to plot all of these subplots anyway. – dalbywh Jul 17 '20 at 21:02
  • @dalbywh--Any chance you know how to expand a single graph to take up all the space on a canvas having a frame as parent? (https://stackoverflow.com/questions/70598913/problem-resizing-plot-on-tkinter-figure-canvas?noredirect=1#comment124899815_70598913) – fishbacp Jan 10 '22 at 21:34

0 Answers0