matplotlib to tkinter code example

Example 1: matplotlib embedded in tkinter

# Creates simple Graph embedded in tkinter

import tkinter
from tkinter import *
import numpy as np
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, NavigationToolbar2Tk
from matplotlib.figure import Figure

tkFenster = Tk()
tkFenster.geometry('400x300')
tkFenster.title('Simple Graph')

figure = Figure(figsize=(4, 3), dpi=100)
x = np.arange(-5, 5, .01)
figure.add_subplot(111).plot(x, x**2)

canvas = FigureCanvasTkAgg(figure, master=tkFenster)
toolbar = NavigationToolbar2Tk(canvas, tkFenster)
toolbar.update()
canvas.draw()
canvas.get_tk_widget().pack(side=tkinter.TOP, fill=tkinter.BOTH, expand=1)

tkFenster.mainloop()

Example 2: how to place a plot in a tkinter frame

from tkinter import *
from pandas import *
from math import sin
import matplotlib.pyplot as plt
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg

# Data for plot
df = DataFrame({'A': range(20),
                'B': [sin(i) for i in range(20)]
                }, index=range(20))  

app = Tk()
app.config(bg='lightgray')
app.title("The title of the project")
app.geometry("800x600")
app.minsize(1000, 700)
app.rowconfigure(0, weight=1)
app.columnconfigure(3, weight=1)

fr_plot = Frame(app)
fr_plot.grid(row=0, column=1, sticky=N+S) 

figure1 = plt.Figure(figsize=(5,4), dpi=80)
ax1 = figure1.add_subplot()
line1 = FigureCanvasTkAgg(figure1, fr_plot)
line1.get_tk_widget().pack(side=TOP, fill=BOTH)
df = df.iloc[:,1]
df.plot(kind='line', legend=True, ax=ax1, color='r', marker='o', fontsize=10)
ax1.set_title('Initial data')

app.mainloop()