2

I have matplotlib script that reads an Excel file and draws it. (as MATPLOT)

And i made another python script looks like a normal program (topmenu, statusbar..) (as GUI)

I want to display MATPLOT in my GUI. Is there any way to call all of the script inside the MATPLOT to run in GUI? Like embedding a video to another website.

root = Tk()
root.geometry("800x600")

#MENU
topMenu = Menu(root)
root.config(menu=topMenu)

loadMenu = Menu(topMenu, tearoff=0)
topMenu.add_cascade(label="File", menu=loadMenu)
loadMenu.add_command(label="Import New", command=doNothing)
loadMenu.add_command(label="Show 'Filter' Menu" , command=showOptions)
loadMenu.add_separator()
loadMenu.add_command(label="Exit", command=root.quit)

#GRAPH
#

#StatusBar
status = Label(root, text="File Name:", bd=1, relief=SUNKEN, anchor=W)
status.pack(side=BOTTOM, fill=X)

root.mainloop()
0

1 Answer 1

7

How to embed a matplotlib Figure in a tkinter Frame

The approach presented subclasses tk.Frame and matplotlib.figure.Figure to make it easy to re-use the code for other purposes.

It creates a tkFrame, with all the boilerplate ready to accept and display a matplotlib Figure.
It then creates a Figure (again, ready to be modified and expanded), and embeds it in the tk.Frame created above.
Last, it launches the tk.mainloop

import matplotlib
import tkinter as tk

matplotlib.use('TkAgg')
# from matplotlib import style
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, NavigationToolbar2TkAgg
from matplotlib.figure import Figure


class GraphPage(tk.Frame):

    def __init__(self, parent):
        tk.Frame.__init__(self, parent)
        self.title_label = tk.Label(self, text="Graph Page Example")
        self.title_label.pack()
        self.pack()

    def add_mpl_figure(self, fig):
        self.mpl_canvas = FigureCanvasTkAgg(fig, self)
        self.mpl_canvas.show()
        self.mpl_canvas.get_tk_widget().pack(side=tk.TOP, fill=tk.BOTH, expand=True)

        self.toolbar = NavigationToolbar2TkAgg(self.mpl_canvas, self)
        self.toolbar.update()
        self.mpl_canvas._tkcanvas.pack(side=tk.TOP, fill=tk.BOTH, expand=True)


class MPLGraph(Figure):

    def __init__(self):
        Figure.__init__(self, figsize=(5, 5), dpi=100)
        self.plot = self.add_subplot(111)
        self.plot.plot([1, 2, 3, 4, 5, 6, 7], [4, 3, 5, 0, 2, 0, 6])


fig = MPLGraph()

root = tk.Tk()
graph_page = GraphPage(root)
graph_page.add_mpl_figure(fig)

root.mainloop()

matplotlib Figure embedded in tkinter Frame

Sign up to request clarification or add additional context in comments.

4 Comments

I know that I can draw lines and create charts. But I want it to be able to read the changes in an Excel file. So if i change the Excel file -or import one from another place if i can do- the gui side will also change. The next thing i want to add is a filter menu that i can select which name to display. (for example: two person and their statistics about how many KM they run each year.)
Surely, this is not the question you asked above, which is: "Is there any way to call all of the script inside the MATPLOT to run in GUI? Like embedding a video to another website"
I think that maybe, you want to ask a new, more specific question, that explains precisely what behavior you would like to have, and describe the output you want. You'll probably get a better response if you post the code for your MATPLOT script.
Just a little change: use NavigationToolbar2Tk instead of NavigationToolbar2TkAgg. For details: What to use instead of NavigationToolbar2TkAgg?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.