10

I have a program that processes data then plots the result. Usually in Python plots are shown in a new window, but I want to show the plot in the same Tkinter window. I've searched and found these two answers, Python Tkinter Embed Matplotlib in GUI, and How do I refresh a matplotlib plot in a Tkinter window?. They state that we should first make a canvas then place it on the window using grid or pack. This is the first time I use canvas, I've applied the concept, and here is a working sample code for that,

from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg

__author__ = 'Dania'
import numpy as np
from Tkinter import *
import matplotlib.pyplot as plt
class mclass:
    def __init__(self,  window):
        self.box = Entry(window)
        self.button = Button (window, text="check", command=self.plot)
        self.box.pack ()
        self.button.pack()

    def plot (self):
        x=np.array ([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
        v= np.array ([16,16.31925,17.6394,16.003,17.2861,17.3131,19.1259,18.9694,22.0003,22.81226])
        p= np.array ([16.23697,     17.31653,     17.22094,     17.68631,     17.73641 ,    18.6368,
            19.32125,     19.31756 ,    21.20247  ,   22.41444   ,  22.11718  ,   22.12453])

        plt.scatter(v,x,color='red')
        plt.plot(p, range(2 +max(x)),color='blue')
        plt.gca().invert_yaxis()

        plt.suptitle ("Estimation Grid", fontsize=16)
        plt.ylabel("Y", fontsize=14)
        plt.xlabel("X", fontsize=14)
        plt.show()
        plt.gcf().canvas.draw()
        fig = plt.figure()
        canvas = FigureCanvasTkAgg(fig, master=window)
        canvas.get_tk_widget().grid(row=1,column=24)
        canvas.draw()

window= Tk()
start= mclass (window)
window.mainloop()

The code above first shows the plot in a separated window, then shows a gray canvas without a plot in the position specified in grid. Also, the canvas expands vertically and a part of the window disappears, in other words the canvas pushes the contents bellow it down, and they don't appear anymore as no vertical scrolling bar appear.

I want to show the plot in the same Tkinter window, and if any content is pushed down I want to be able to scroll the window.

pppery
  • 3,731
  • 22
  • 33
  • 46
Dania
  • 1,648
  • 4
  • 31
  • 57

1 Answers1

26

Main changes:

  1. Using matplolib.figure instead of matplotlib.pyplot (and renaming all related functions)
  2. Adding a self.window variable
  3. Using pack() everywhere (you cannot mix grid and pack in one container.)

Result:

__author__ = 'Dania'
import matplotlib
matplotlib.use('TkAgg')
import numpy as np
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
from matplotlib.figure import Figure
from Tkinter import *

class mclass:
    def __init__(self,  window):
        self.window = window
        self.box = Entry(window)
        self.button = Button (window, text="check", command=self.plot)
        self.box.pack ()
        self.button.pack()

    def plot (self):
        x=np.array ([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
        v= np.array ([16,16.31925,17.6394,16.003,17.2861,17.3131,19.1259,18.9694,22.0003,22.81226])
        p= np.array ([16.23697,     17.31653,     17.22094,     17.68631,     17.73641 ,    18.6368,
            19.32125,     19.31756 ,    21.20247  ,   22.41444   ,  22.11718  ,   22.12453])

        fig = Figure(figsize=(6,6))
        a = fig.add_subplot(111)
        a.scatter(v,x,color='red')
        a.plot(p, range(2 +max(x)),color='blue')
        a.invert_yaxis()

        a.set_title ("Estimation Grid", fontsize=16)
        a.set_ylabel("Y", fontsize=14)
        a.set_xlabel("X", fontsize=14)

        canvas = FigureCanvasTkAgg(fig, master=self.window)
        canvas.get_tk_widget().pack()
        canvas.draw()

window= Tk()
start= mclass (window)
window.mainloop()
Eric Levieil
  • 3,554
  • 2
  • 13
  • 18
  • Thanks a lot Eric, this is working perfectly. There are two lines which are not clear to me, can you please tell me what does these two lines mean, fig = Figure(figsize=(6,6)) and a = fig.add_subplot(111)? Thank you. – Dania Jul 16 '15 at 12:29
  • I started from http://stackoverflow.com/questions/4073660/python-tkinter-embed-matplotlib-in-gui where they make a figure then a subfigure then add the figure to the canvas. Hope this helps. – Eric Levieil Jul 16 '15 at 12:59
  • Hey, I know that I am years late to this conversation, but if I wanted to clear the plot with a command, how would I do that? – Aaron England Dec 25 '18 at 18:50
  • I am trying to make an interactive scatter graph (with some click event) but it does not work with Figure :( –  Dec 30 '18 at 22:05