How to Add Matplotlib in TKinter GUI Window

In this lesson i want to show you How to Add Matplotlib in TKinter GUI Window, first let’s talk about matplotlib and tkinter.

 

What is TKinter ?

Tkinter is standard GUI (Graphical User Interface) library for Python. It provides set of tools for creating graphical user interfaces that are portable across multiple platforms, including Windows, macOS and Linux. Tkinter provides simple and intuitive way to create windows, dialogs, buttons, labels and other GUI elements using Python. with Tkinter you can create  different applications from simple desktop applications to more complex ones such as data visualization tools, games and multimedia players. Tkinter is included with most Python installations and is easy to learn and use, making it a popular choice for many Python developers.

 

 

What is Matplotlib ?

Matplotlib is plotting library for Python programming language. It provides different types of  visualizations including line charts, bar charts, scatter plots and many more. Matplotlib is highly customizable and it allows you to control every aspect of the appearance of your charts and plots. also it can be used in different contexts, including in Jupyter notebooks, in scripts or in GUI applications such as Tkinter.

Matplotlib is often used to visualize data in scientific, financial and engineering applications, but it can be used for different other purposes as well. with its rich set of features and its ability to produce high-quality output, Matplotlib is powerful tool for data visualization and exploration.

 

 

How to Install Matplotlib ?

Matplotlib can be installed using the Python package manager pip. this is how to install it:

  1. Open a terminal or command prompt window.
  2. Run the following command: pip install matplotlib

This will download and install the latest version of Matplotlib and all its dependencies. Once the installation is complete, you can start using Matplotlib in your Python scripts and projects.

also you can install Matplotlib using package manager for your operating system, such as apt for Ubuntu or Homebrew for macOS. 

 

How to Add Matplotlib in TKinter GUI Window

To add charts graphs and images to Tkinter applications, you can use different external libraries and tools.these are some popular options:

  • Matplotlib: Matplotlib is plotting library for Python that provides different types visualizations, including line charts, bar charts, scatter plots and many more. you can easily embed Matplotlib charts in Tkinter applications using the matplotlib.backends.backend_tkagg backend.
  • Pygal: Pygal is Python library for creating vector graphics including bar charts, line charts and many more. It provides simple and clean API that is easy to use and produces high quality output.
  • Pillow: Pillow is Python Imaging Library that provides support for different image formats, including PNG, JPEG and GIF. You can use Pillow to load, manipulate and display images in Tkinter applications.

Here’s an example of how to use Matplotlib to create a bar chart in a Tkinter application:

In this example, the show_chart function creates bar chart using Matplotlib and displays it in Tkinter window. the chart is displayed using the FigureCanvasTkAgg backend which allows you to embed Matplotlib charts in Tkinter applications. Note that you can use similar techniques with other libraries, such as Pygal or Pillow, to display different types of charts and images in Tkinter applications.

 

 

 

Run the complete code and this will be the result.

How to Add Matplotlib in TKinter GUI Window
How to Add Matplotlib in TKinter GUI Window

 

 

Learn More on Python

 

This is an example of how to create a bar chart using Object Oriented Programming (OOP) with Matplotlib in Tkinter:

 

Leave a Comment