How to Embed Matplotlib in TKinter Window

In this Python TKinter article i want to show How To Embed Matplotlib In TKinter Window. now first we want to talk about TKinter and also Matplotlib.

 

 

What is TKinter?

Tkinter is standard Python library, and it is used to build graphical user interfaces (GUIs). The name of Tkinter comes from Tk interface,  and it provides Python bindings for Tk GUI toolkit. Tkinter is included with most Python installations, and you don’t need to install TKinter. using TKinter you can create desktop applications for different platforms, including Windows, macOS and Linux.

 

 

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.

 

 

How to Install Matplotlib?

You can install matplotib using pip, open your command prompt or terminal and write this command.

 

 

 

How to Embed Matplotlib in TKinter Window?

Now let’s create an example of embedding matplotlib in TKinter, this is the complete code for this article

 

Now let’s describe the code

 

We import necessary modules:

  • tkinter: Provides GUI functionalities.
  • Figure from matplotlib.figure: Represents a Matplotlib figure.
  • FigureCanvasTkAgg and NavigationToolbar2Tk from matplotlib.backends.backend_tkagg: Provide tools for embedding Matplotlib plots into Tkinter applications.

 

 

Root Class:

  • We have defined a class Root that inherits from Tk, and it is the main window of our Tkinter application.
  • In the __init__ method, we initialize the window by setting its title and icon, after that we call the matplotCanvas method.

 

 

MatplotCanvas Method:

  • This method creates and embeds a Matplotlib plot into Tkinter window.
  • It creates a Matplotlib figure (f) with specific size and dpi, also we add a subplot (a) to it, and plots some data on the subplot.
  • After that it creates a Tkinter compatible canvas (canvas) from Matplotlib figure using FigureCanvasTkAgg.
  • The canvas is drawn (canvas.draw()) and packed into the bottom side of the Tkinter window to fill the available space.
  • Also a navigation toolbar (toolbar) is created using NavigationToolbar2Tk and packed into the top side of the window.

 

 

Creating TKiner Application Instance and Running the Event Loop:

  • An instance of the Root class is created.
  • Tkinter event loop is started (root.mainloop()), which listens for events such as button clicks and updates the GUI accordingly.

 

 

 

 

Run the complete code and this will be the result.

How to Embed Matplotlib in TKinter Window
How to Embed Matplotlib in TKinter Window

 

 

 

 

FAQs:

 

How to combine Matplotlib and Tkinter?

Combining Matplotlib and Tkinter allows you to create GUI applications with interactive plots. This is a basic example of how to do it:

 

 

This will be the result

TKinter and Matplotlib
TKinter and Matplotlib

 

 

 

How do I show Matplotlib in Python?

For displaying Matplotlib plot in Python, you can use show() function provided by Matplotlib. This is a simple example:

 

Subscribe and Get Free Video Courses & Articles in your Email

 

Leave a Comment

Share via
Copy link
Powered by Social Snap
×