top of page
Subham Das

How to embed the PyPlot graphs inside the Tkinter frame?

Updated: Mar 31, 2020

This program will help you to insert any PyPlot graphs inside Tkinter window. First we are creating a figure using matplotlib library. Then we are creating an object inside the figure using the command "add_subplot()". Now we can use that object for plotting graphs. Here I have used line graph but you can use pie or bar graph instead of line graph.

But here is a problem that we cannot directly embed that figure inside tkinter window. So I have created a canvas and I fix that figure inside the canvas object. At last I just get that the canvas and packed it inside the tkinter window.


# Embedding the graphs in tkinter window

from tkinter import * import matplotlib from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg from matplotlib.figure import Figure

# Creating tkinter window win = Tk()

# Creating Figure. fig = Figure(figsize = (5,5), dpi = 100) x = [1,2,3,4,5] y = ["Debdut", "Sayoni", "Aishwarya", "Ankit", "Brata"]

# Plotting the graph inside the Figure a = fig.add_subplot(122) a.plot(x,y, marker = "o", label = "October") a.set_xlabel("Marks") a.set_ylabel("Students") a.set_title("Graph_Tk") a.legend() a.grid()

# Creating Canvas canv = FigureCanvasTkAgg(fig, master = win) canv.draw() get_widz = canv.get_tk_widget() get_widz.pack() win.mainloop()




6,724 views1 comment

Recent Posts

See All

1 Comment


hedjamohammmed
Aug 04, 2021

tank's for help, this code was usufull

Like
logo12.PNG
bottom of page