在tkinter GUI中更新matplotlib图 [英] update matplotlib plot in tkinter GUI

查看:85
本文介绍了在tkinter GUI中更新matplotlib图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在tkinter GUI中更新matplotlib图。我在下面的代码示例中尝试这样做。

I wanna update a matplotlib plot in a tkinter GUI. 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()

目前的问题是,plot函数中的ax对象是未知的。如果我尝试plot(self,canvas,ax),则GUI无法打开。只有一个图形可以绘制数据。

At the moment the problem is, that the ax object is not known in the plot function. If I try plot(self,canvas,ax) the GUI does not open. Only a figure that plots the data.

我想在图形用户界面中绘制数据。至少刷新频率约为3-5Hz。
原因我是一个绝对的初学者,这种代码解决方案可能不是最好的方法。因此,如果有人可以向我展示更智能的解决方案,我将很高兴。

I wanna plot the data in the figure that is seen in the GUI. At least with a refresh rate round about 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.

谢谢!

推荐答案

感谢Abishek抽出宝贵的时间发帖

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

我刚刚对您的答案做了一些修改,以便它可以作为独立模块运行,而无需sys.stdout的输入。还更改了python 2.7的tkinter导入

I've just modified your answer a bit so that it runs as a standalone module without needing input from sys.stdout. Also changed the tkinter imports for python 2.7

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

这篇关于在tkinter GUI中更新matplotlib图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆