Links

How to embed Matplotlib charts in Tkinter GUI? - GeeksforGeeks

Code

From the link, we get

from tkinter import *
from matplotlib.figure import Figure
from matplotlib.backends.backend_tkagg import (FigureCanvasTkAgg,
NavigationToolbar2Tk)

# plot function is created for
# plotting the graph in
# tkinter window
def plot():

	# the figure that will contain the plot
	fig = Figure(figsize = (5, 5),
				dpi = 100)

	# list of squares
	y = [i**2 for i in range(101)]

	# adding the subplot
	plot1 = fig.add_subplot(111)

	# plotting the graph
	plot1.plot(y)

	# creating the Tkinter canvas
	# containing the Matplotlib figure
	canvas = FigureCanvasTkAgg(fig, master = window)
	canvas.draw()

	# placing the canvas on the Tkinter window
	canvas.get_tk_widget().pack()

	# creating the Matplotlib toolbar
	toolbar = NavigationToolbar2Tk(canvas, window)
	toolbar.update()

	# placing the toolbar on the Tkinter window
	canvas.get_tk_widget().pack()

# the main Tkinter window
window = Tk()

# setting the title
window.title('Plotting in Tkinter')

# dimensions of the main window
window.geometry("500x500")

# button that displays the plot
plot_button = Button(
		master = window,
		command = plot,
		height = 2,
		width = 10,
		text = "Plot"
)

# place the button
# in main window
plot_button.pack()

# run the gui
window.mainloop()

where it seems the most important part is creating the Matplotlib toolbar in the GUI then putting in the plot using

# creating the Tkinter canvas
# containing the Matplotlib figure
canvas = FigureCanvasTkAgg(fig, master = window)
canvas.draw()

# placing the canvas on the Tkinter window
canvas.get_tk_widget().pack()

# creating the Matplotlib toolbar
toolbar = NavigationToolbar2Tk(canvas, window)
toolbar.update()

# placing the toolbar on the Tkinter window
canvas.get_tk_widget().pack()

where the imporant thing is that they pass the figure to the GUE usine FigureCanvasTkAgg. Also note the imports,

from tkinter import *
from matplotlib.figure import Figure
from matplotlib.backends.backend_tkagg import (FigureCanvasTkAgg, NavigationToolbar2Tk)

where they import matplotlib.figure instead of matplotlib.pyplot