Refresh matplotlib plot in tkinter GUI

I want to update matplotlib plot in tkinter GUI. I tried to do it in the following code example.

import matplotlib
matplotlib.use('TkAgg')
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, NavigationToolbar2TkAgg
from matplotlib.figure import Figure
import tkinter as tk
import tkinter.ttk as ttk
import sys

class Application(tk.Frame):
    def __init__(self, master=None):
        tk.Frame.__init__(self,master)
        self.createWidgets()

    def createWidgets(self):
       fig=plt.figure(figsize=(8,8))
       ax=fig.add_axes([0.1,0.1,0.8,0.8],polar=True)
       canvas=FigureCanvasTkAgg(fig,master=root)
       canvas.get_tk_widget().grid(row=0,column=1)
       canvas.show()

       self.plotbutton=tk.Button(master=root, text="plot", command=self.plot)
       self.plotbutton.grid(row=0,column=0)

    def plot(self):
       for line in sys.stdout: #infinite loop, reads data of a subprocess
           theta=line[1]
           r=line[2]
           ax.plot(theta,r,linestyle="None",maker='o')
           plt.show(block=False)
           plt.pause(0.001)
           plt.cla()
           #here set axes

root=tk.Tk()
app=Application(master=root)
app.mainloop()

      

The problem at the moment is that the ax object is not known in the plot function. If I try plot (self, canvas, ax) the GUI won't open. Only the shape that displays data.

I want to write data in a figure that is displayed in the GUI. At least the refresh rate is around 3-5 Hz. Because I'm a complete newbie, this code solution is probably not the best way to do it. So I would be happy if someone could show me a smarter solution.

Thank!

+3


source to share


2 answers


Ok, I could work it out myself. Here's the solution:



import matplotlib
matplotlib.use('TkAgg')
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, NavigationToolbar2TkAgg
from matplotlib.figure import Figure
import tkinter as tk
import tkinter.ttk as ttk
import sys

class Application(tk.Frame):
    def __init__(self, master=None):
        tk.Frame.__init__(self,master)
        self.createWidgets()

    def createWidgets(self):
        fig=plt.figure(figsize=(8,8))
        ax=fig.add_axes([0.1,0.1,0.8,0.8],polar=True)
        canvas=FigureCanvasTkAgg(fig,master=root)
        canvas.get_tk_widget().grid(row=0,column=1)
        canvas.show()

        self.plotbutton=tk.Button(master=root, text="plot", command=lambda: self.plot(canvas,ax))
        self.plotbutton.grid(row=0,column=0)

    def plot(self,canvas,ax):
        for line in sys.stdout: #infinite loop, reads data of a subprocess
            theta=line[1]
            r=line[2]
            ax.plot(theta,r,linestyle="None",maker='o')
            canvas.draw()
            ax.clear()
            #here set axes

root=tk.Tk()
app=Application(master=root)
app.mainloop()

      

+3


source


Thanks to Abishek for taking the time to post an answer to your own problem.

I only slightly modified your answer to work as a standalone module without any input from sys.stdout. Also tkinter import for python 2.7 changed



import matplotlib
matplotlib.use('TkAgg')
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, NavigationToolbar2TkAgg
from matplotlib.figure import Figure
import Tkinter as tk  # python 2.7
import ttk            # python 2.7
import sys

class Application(tk.Frame):
    def __init__(self, master=None):
        tk.Frame.__init__(self,master)
        self.createWidgets()

    def createWidgets(self):
        fig=plt.figure(figsize=(8,8))
        ax=fig.add_axes([0.1,0.1,0.8,0.8],polar=True)
        canvas=FigureCanvasTkAgg(fig,master=root)
        canvas.get_tk_widget().grid(row=0,column=1)
        canvas.show()

        self.plotbutton=tk.Button(master=root, text="plot", command=lambda: self.plot(canvas,ax))
        self.plotbutton.grid(row=0,column=0)

    def plot(self,canvas,ax):
        c = ['r','b','g']  # plot marker colors
        ax.clear()         # clear axes from previous plot
        for i in range(3):
            theta = np.random.uniform(0,360,10)
            r = np.random.uniform(0,1,10)
            ax.plot(theta,r,linestyle="None",marker='o', color=c[i])
            canvas.draw()

root=tk.Tk()
app=Application(master=root)
app.mainloop()

      

+2


source







All Articles