In this Python TKinter article i want to show How To Embed Matplotlib In TKinter Window.
if you are interested in Python GUI Development, you can 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 you can read more articles on Python GUI Development
1: PyQt5 GUI Development Tutorials
2: Pyside2 GUI Development Tutorials
3: wxPython GUI Development Tutorials
4: Kivy GUI Development Tutorials
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
Subscribe and Get Free Video Courses & Articles in your Email