In this Python TKinter article i want to show How To Embed Matplotlib In TKinter Window. for more TKinter articles check the below links.
What Is Matplotlib ?
Matplotlib is a Python 2D plotting library which produces publication quality figures in a variety of hardcopy formats and interactive environments across platforms. Matplotlib can be used in Python scripts, the Python and IPython shells, the Jupyter notebook, web application servers, and four graphical user interface toolkits.
Also check more tkinter articles
1: Introduction To TKinter GUI Development
2: How To Create Label In Python TKinter
3: How To Create GUI Button In TKinter
4: How To Create TextBox In TKinter
5: How To Create ComboBox In TKinter
6: How To Create CheckButton In Python TKinter
7: How To Create RadioButton In Python TKinter
8: How To Create ScrollTextArea In TKinter
9: Python TKinter Creating LabelFrame
10: How To Create MenuItem In TKinter
11: Python TKinter Creating MenuItem Events
12: How To Create TabWidgets In TKinter
13: How Add Widgets In TKinter Tab Controls
14: How To Create MessageBox In TKinter
15: How To Create MultiChoiceBox In Python TKinter
16: How To Create ProgressBar In Python TKinter
17: How To Create ProgressBar In Python TKinter
18: How To Create Canvas In Python TKinter
So now this is the complete code for How To Embed Matplotlib In TKinter Window. make sure that you have installed Matplotlib in your computer . if you have not installed, you can installed by this command pip install matplotlib.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
from tkinter import * from matplotlib.figure import Figure from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, NavigationToolbar2TkAgg class Root(Tk): def __init__(self): super(Root, self).__init__() self.title("Python Tkinter Embeding Matplotlib") self.minsize(640, 400) self.wm_iconbitmap('icon.ico') self.matplotCanvas() def matplotCanvas(self): f = Figure(figsize=(5,5), dpi=100) a = f.add_subplot(111) a.plot([1, 2, 3, 4, 5, 6, 7, 8], [5, 6, 1, 3, 8, 9, 3, 5]) canvas = FigureCanvasTkAgg(f, self) canvas.show() canvas.get_tk_widget().pack(side=BOTTOM, fill=BOTH, expand=True) toolbar = NavigationToolbar2TkAgg(canvas, self) toolbar.update() canvas._tkcanvas.pack(side=TOP, fill=BOTH, expand=True) root = Root() root.mainloop() |
So at the top we have created our Root class that extends from TK, and we have added our window requirements like title, size and icon in the constructor of the class.
|
1 2 3 4 5 6 7 8 9 |
class Root(Tk): def __init__(self): super(Root, self).__init__() self.title("Python Tkinter Embeding Matplotlib") self.minsize(640, 400) self.wm_iconbitmap('icon.ico') self.matplotCanvas() |
After that we have created our def matplotCanvas() method. in this method we have created our Figure also we have created the values that we want to plot. the important point is this that you need to created the object of FigureCanvasTKAgg().
|
1 2 3 4 5 6 7 8 9 10 11 |
def matplotCanvas(self): f = Figure(figsize=(5,5), dpi=100) a = f.add_subplot(111) a.plot([1, 2, 3, 4, 5, 6, 7, 8], [5, 6, 1, 3, 8, 9, 3, 5]) canvas = FigureCanvasTkAgg(f, self) canvas.show() canvas.get_tk_widget().pack(side=BOTTOM, fill=BOTH, expand=True) toolbar = NavigationToolbar2TkAgg(canvas, self) toolbar.update() canvas._tkcanvas.pack(side=TOP, fill=BOTH, expand=True) |
This line of code is for creating NavigationToolbar for matplotlib window and we pass the object of canvas in that
|
1 |
toolbar = NavigationToolbar2TkAgg(canvas, self) |
Run the complete code and this will be the result

Also you can watch the complete video for this article