How to Place Matplotlib Charts on a Tkinter GUI

Often times you may need to place matplotlib charts on a tkinter GUI. This feature is especially useful for users who deal with front-end GUIs.

And so, in this tutorial, I’ll show you the steps to place matplotlib charts on a tkinter GUI.

More specifically, I’ll show you how to embed the following charts on your GUI:

  • Bar
  • Line
  • Scatter

By the end of this tutorial, you will be able to create the following tkinter GUI with the embedded charts:

 

How to Place Matplotlib Charts on a Tkinter GUI

 

Let’s now review the steps to achieve this goal.

Steps to place matplotlib charts on a tkinter GUI

Step-1: Prepare the data-sets for the charts

Firstly, you’ll need to prepare the data-sets to be used as the input for our charts.

For illustration purposes, I created the following 3 data-sets for our charts:

Data for the Bar Chart

CountryGDP_Per_Capita
USA45000
Canada42000
Germany52000
UK49000
France47000

Data for the Line Chart

YearUnemployment_Rate
19209.8
193012
19408
19507.2
19606.9
19707
19806.5
19906.2
20005.5
20106.3

Data for the Scatter Diagram

Interest_RateStock_Index_Price
51500
5.51520
61525
5.51523
5.251515
6.51540
71545
81560
7.51555
8.51565

Step-2: Create the DataFrame in Python

Next, you may utilize pandas DataFrame to capture that data in Python.

Here is the generic structure that you can use to create a DataFrame in Python:

 

Data = {'First Field Name': ['First value', 'Second value',…],
        'Second Field Name': ['First value', 'Second value',…],
         …
        }

df = DataFrame (Data, columns = ['First Field Name','Second Field Name',…])

 

For our example, the data-sets can be capture as follows:

 

from pandas import DataFrame

   
Data1 = {'Country': ['US','CA','GER','UK','FR'],
        'GDP_Per_Capita': [45000,42000,52000,49000,47000]
       }

df1 = DataFrame(Data1, columns= ['Country', 'GDP_Per_Capita'])
df1 = df1[['Country', 'GDP_Per_Capita']].groupby('Country').sum()



Data2 = {'Year': [1920,1930,1940,1950,1960,1970,1980,1990,2000,2010],
        'Unemployment_Rate': [9.8,12,8,7.2,6.9,7,6.5,6.2,5.5,6.3]
       }
  
df2 = DataFrame(Data2,columns=['Year','Unemployment_Rate'])
df2 = df2[['Year', 'Unemployment_Rate']].groupby('Year').sum()



Data3 = {'Interest_Rate': [5,5.5,6,5.5,5.25,6.5,7,8,7.5,8.5],
        'Stock_Index_Price': [1500,1520,1525,1523,1515,1540,1545,1560,1555,1565]
       }
  
df3 = DataFrame(Data3,columns=['Interest_Rate','Stock_Index_Price'])

 

You may want to check the following link which further explains how to create pandas DataFrame to capture your data in Python.

Step-3: Create the Python GUI

Next, you’ll need to create the Python GUI, so that you can place the charts on it.

To begin, you’ll need to import the tkinter and matplotlib modules like this:

 

import tkinter as tk
import matplotlib.pyplot as plt
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg

 

Then, add the charts to the GUI by using this generic structure:

 

figure = plt.Figure(figsize=(6,5), dpi=100)
ax = figure.add_subplot(111)
chart_type = FigureCanvasTkAgg(figure, root)
chart_type.get_tk_widget().pack()
df.plot(kind='Chart Type such as bar', legend=True, ax=ax)
ax.set_title('The Title of your chart')

 

Slight variations may be applied to the above structure, depending on the chart that you need to plot.

Putting everything together, your full Python code should look like this:

 

import tkinter as tk
from pandas import DataFrame
import matplotlib.pyplot as plt
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg

   
Data1 = {'Country': ['US','CA','GER','UK','FR'],
        'GDP_Per_Capita': [45000,42000,52000,49000,47000]
       }

df1 = DataFrame(Data1, columns= ['Country', 'GDP_Per_Capita'])
df1 = df1[['Country', 'GDP_Per_Capita']].groupby('Country').sum()



Data2 = {'Year': [1920,1930,1940,1950,1960,1970,1980,1990,2000,2010],
        'Unemployment_Rate': [9.8,12,8,7.2,6.9,7,6.5,6.2,5.5,6.3]
       }
  
df2 = DataFrame(Data2,columns=['Year','Unemployment_Rate'])
df2 = df2[['Year', 'Unemployment_Rate']].groupby('Year').sum()



Data3 = {'Interest_Rate': [5,5.5,6,5.5,5.25,6.5,7,8,7.5,8.5],
        'Stock_Index_Price': [1500,1520,1525,1523,1515,1540,1545,1560,1555,1565]
       }
  
df3 = DataFrame(Data3,columns=['Interest_Rate','Stock_Index_Price'])
 
  

root= tk.Tk() 
  

figure1 = plt.Figure(figsize=(6,5), dpi=100)
ax1 = figure1.add_subplot(111)
bar1 = FigureCanvasTkAgg(figure1, root)
bar1.get_tk_widget().pack(side=tk.LEFT, fill=tk.BOTH)
df1.plot(kind='bar', legend=True, ax=ax1)
ax1.set_title('Country Vs. GDP Per Capita')


figure2 = plt.Figure(figsize=(5,4), dpi=100)
ax2 = figure2.add_subplot(111)
line2 = FigureCanvasTkAgg(figure2, root)
line2.get_tk_widget().pack(side=tk.LEFT, fill=tk.BOTH)
df2.plot(kind='line', legend=True, ax=ax2, color='r',marker='o', fontsize=10)
ax2.set_title('Year Vs. Unemployment Rate')


figure3 = plt.Figure(figsize=(5,4), dpi=100)
ax3 = figure3.add_subplot(111)
ax3.scatter(df3['Interest_Rate'],df3['Stock_Index_Price'], color = 'g')
scatter3 = FigureCanvasTkAgg(figure3, root) 
scatter3.get_tk_widget().pack(side=tk.LEFT, fill=tk.BOTH)
ax3.legend() 
ax3.set_xlabel('Interest Rate')
ax3.set_title('Interest Rate Vs. Stock Index Price')

root.mainloop()

Step-4: Run the Python code

Run the above Python code, and you’ll see the matplotlib charts placed on the GUI:

 

How to Place Matplotlib Charts on a Tkinter GUI

 

That’s it! For additional information about the tkinter module, you may check the tkinter documentation.