How to embed Matplotlib charts in Tkinter GUI? Last Updated : 10 Jul, 2020 Comments Improve Suggest changes Like Article Like Report Prerequisite: Introduction to Tkinter | Introduction to Matplotlib When Matplotlib is used from Python shell, the plots are displayed in a default window. The plots can be embedded in many graphical user interfaces like wxpython, pygtk, or Tkinter. These various options available as a target for the output plot are referred to as 'backends'. There are various modules available in matplotlib.backend for choosing the backend. One such module is backend_tkagg which is useful for embedding plots in Tkinter. Creating the Tkinter Application : First, let us create a basic Tkinter application with the main window and one button which can be used to display the plot. Python3 # import all classes/methods # from the tkinter module from tkinter import * # The main tkinter window window = Tk() # setting the title and window.title('Plotting in Tkinter') # setting the dimensions of # the main window window.geometry("500x500") # button that would displays the plot plot_button = Button(master = window, height = 2, width = 10, text = "Plot") # place the button # into the window plot_button.pack() # run the gui window.mainloop() Output : Embedding the Plot: First, we need to create the figure object using the Figure() class. Then, a Tkinter canvas(containing the figure) is created using FigureCanvasTkAgg() class. Matplotlib charts by default have a toolbar at the bottom. When working with Tkinter, however, this toolbar needs to be embedded in the canvas separately using the NavigationToolbar2Tk() class.In the implementation below, a simple graph for: y = x^2 is plotted. The plot function is bound to a button that displays the figure when pressed. Python3 from tkinter import * from matplotlib.figure import Figure from matplotlib.backends.backend_tkagg import (FigureCanvasTkAgg, NavigationToolbar2Tk) # plot function is created for # plotting the graph in # tkinter window def plot(): # the figure that will contain the plot fig = Figure(figsize = (5, 5), dpi = 100) # list of squares y = [i**2 for i in range(101)] # adding the subplot plot1 = fig.add_subplot(111) # plotting the graph plot1.plot(y) # creating the Tkinter canvas # containing the Matplotlib figure canvas = FigureCanvasTkAgg(fig, master = window) canvas.draw() # placing the canvas on the Tkinter window canvas.get_tk_widget().pack() # creating the Matplotlib toolbar toolbar = NavigationToolbar2Tk(canvas, window) toolbar.update() # placing the toolbar on the Tkinter window canvas.get_tk_widget().pack() # the main Tkinter window window = Tk() # setting the title window.title('Plotting in Tkinter') # dimensions of the main window window.geometry("500x500") # button that displays the plot plot_button = Button(master = window, command = plot, height = 2, width = 10, text = "Plot") # place the button # in main window plot_button.pack() # run the gui window.mainloop() Output : Comment More infoAdvertise with us Next Article How to embed Matplotlib charts in Tkinter GUI? C cosine1509 Follow Improve Article Tags : Python Python-matplotlib Python-tkinter Practice Tags : python Similar Reads How to add a grid on a figure in Matplotlib ? Matplotlib library is widely used for plotting graphs. In many graphs, we require to have a grid to improve readability. Grids are created by using grid() function in the Pyplot sublibrary. In this article, we will see how to add grid in Matplotlb. Add a Grid on a Figure in MatplotlibBelow are the w 3 min read How to add PDF in Tkinter GUI Python ? In this article, We are going to see how to add a PDF file Tkinter GUI, For that, we don't have a direct widget to do this. For that, We need to have python version 2.7 or more. And you need to install the 'tkPDFViewer' library. This library allows you to embed the PDF file in your Tkinter GUI. Inst 2 min read How to Create an Empty Figure with Matplotlib in Python? Creating a figure explicitly is an object-oriented style of interfacing with matplotlib. The figure is a basic building block of creating a plot as Matplotlib graphs our data on figures. This figure keeps track of all other components such as child axes, legends, title, axis, etc. Steps to create an 2 min read Create Scatter Charts in Matplotlib using Flask In this article, we will see how to create charts in Matplotlib with Flask. We will discuss two different ways how we can create Matplotlib charts in Flask and present it on an HTML webpage with or without saving the plot using Python. File structure  Create and Save the Plot in the Static Directory 4 min read How to Create Matplotlib Plots Without a GUI To create and save plots using Matplotlib without opening a GUI window, you need to configure Matplotlib to use a non-interactive backend. This can be achieved by setting the backend to 'Agg', which is suitable for generating plots without displaying them. Let's see how to set the backend to Agg: Me 2 min read Line chart in Matplotlib - Python Matplotlib is a data visualization library in Python. The pyplot, a sublibrary of Matplotlib, is a collection of functions that helps in creating a variety of charts. Line charts are used to represent the relation between two data X and Y on a different axis. In this article, we will learn about lin 6 min read How to add Matplotlib graph in Kivy ? In this article, we will discuss how to add matplotlib graph in the kivy app. Approach:Import matplotlib pyplotImport numpyImport FigureCanvas KivyAggImport kivy appImport kivy builderCreate App classReturn builder stringRun an instance of the class Below is the Implementation. Python3 # importing p 2 min read How to add text to Matplotlib? Matplotlib is a plotting library in Python to visualize data, inspired by MATLAB, meaning that the terms used (Axis, Figure, Plots) will be similar to those used in MATLAB. Pyplot is a module within the Matplotlib library which is a shell-like interface to Matplotlib module.  It provides almost any 5 min read How To Add An Image In Tkinter? Python Tkinter supports various image formats such as PNG, JPEG, GIF, and BMP. Images are handled in Tkinter using the PhotoImage and BitmapImage classes and the Pillow Library. The PhotoImage class is more commonly used due to its support for color images, whereas BitmapImage is limited to monochro 4 min read Like