简体   繁体   English

更新tkinter GUI中的matplotlib图

[英]update matplotlib plot in tkinter GUI

I wanna update a matplotlib plot in a tkinter GUI. 我想在tkinter GUI中更新matplotlib图。 I tried to do so 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()

At the moment the problem is, that the ax object is not known in the plot function. 目前问题是,在绘图函数中不知道ax对象。 If I try plot(self,canvas,ax) the GUI does not open. 如果我尝试绘图(自我,画布,斧头),GUI就不会打开。 Only a figure that plots the data. 只是绘制数据的图形。

I wanna plot the data in the figure that is seen in the GUI. 我想绘制GUI中图中的数据。 At least with a refresh rate round about 3-5Hz. 至少刷新率约为3-5Hz。 Cause I am an absolute beginner this code solution is probably not the best way to do so. 因为我是一个绝对的初学者,这个代码解决方案可能不是最好的方法。 So I would be happy, if someone could show me a smarter solution. 如果有人能告诉我一个更聪明的解决方案,我会很高兴。

Thanks! 谢谢!

Ok I could solve it myself. 好的,我可以自己解决。 Here 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()

Thanks Abishek for taking the time to post the answer to your own problem. 感谢Abishek花时间发布您自己问题的答案。

I've just modified your answer a bit so that it runs as a standalone module without needing input from sys.stdout. 我刚刚修改了你的答案,所以它作为一个独立的模块运行,无需sys.stdout的输入。 Also changed the tkinter imports for python 2.7 还更改了python 2.7的tkinter导入

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()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM