使用matplotlib绘制Tkinter中的数据 - 在列表之间切换 [英] Plotting data in Tkinter with matplotlib - switching between lists

查看:320
本文介绍了使用matplotlib绘制Tkinter中的数据 - 在列表之间切换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个使用 Tkinter matplotlib 。我有2个列表列表(一个为x轴,一个为y轴),我想有一个按钮,可以在列表中的列表之间切换。我从问题中获取了大量代码。基于Tkinter和matplotlib的交互图a>,但我不能得到的按钮工作,因为我喜欢。我很新鲜,使用类和有点难以理解他们。



tft 数据
tf1 是y数据



数据示例:

  x-data = [[1,2,3,4,5],[10,11,13,15,12,19],[20 ,25,27]] 
y-data = [[5.4,6,10,11,6],[4,6,8,34,20,12],[45,25,50]] b $ b

我的代码会在列表中列出一个列表,但不会在列表之间切换该列表当我点击按钮。我已经注释掉了我试过的其他方法。它总是说应用程序没有属性 画布 时,我使用它。就像我说的,我对课程很新,我想更好地理解他们。



我更新了我的代码,以便它现在可以识别 event_num ,并且每当按下按钮时都会输出正确的值。然而,该图不是用新数据更新(即,它继续只显示第一数据集而不是在列表之间切换)。我相信问题是在函数增加减少。我尝试使用 .line,= ax.plot(tft [self.event_num],tf1 [self.event_num],'。') self.canvas.draw c $ c>但它不工作。我正在寻找要编辑的部分,以便图表将更改。

 类应用程序:


def __init __(self,master):
self.event_num = 1
#创建一个容器
frame = Frame(master)
#创建2个按钮
self.button_left = Button(frame,text =< Previous Event,
command = self.decrease)
self.button_left.grid(row = 0,column = 0)
self.button_right = Button(frame,text =Next Event>,
command = self.increase)
self.button_right.grid(row = 0,column = 1)

fig = figure()
ax = fig.add_subplot(111)
fig.autofmt_xdate()
import matplotlib.dates as mdates
ax.fmt_xdata = mdates .DateFormatter('%Y-%m-%d')
self.line,= ax.plot(tft [self.event_num],tf1 [self.event_num],'。')


self.canvas = FigureCanvasTkAgg(fig,master = master)
self.canvas.show()
self.canvas.get_tk_widget()。grid(row = 1,column = 0)
frame.grid(row = 0,column = 0)

def decrease(self):
self.event_num - = 1
print self.event_num
self.line,= ax.plot(tft [self.event_num],tf1 [self.event_num],'。')
self.canvas.draw()
#self.canvas .draw(tft [self.event_num],tf1 [self.event_num],'。')
#self.line.set_xdata(tft [event_num])
#self.line.set_ydata(tf1 [ event_num])


def increase(self):
self.event_num + = 1
print self.event_num
self.line,= ax。 plot(tft [self.event_num],tf1 [self.event_num],'。')
self.canvas.draw()
#self.canvas.draw(tft [self.event_num],tf1 [self.event_num],'。')
#self.set_xdata(tft [event_num])
#self.set_ydata(tf1 [event_num])


root = Tk()
app = App(root)
root.mainloop()


c> c> c> c> c> /分配。



此行:

  self.button_left = text =< Previous Event,
command = self.decrease(event_num))

正在调用减少方法,因为您使用括号和提供的参数,而不是仅绑定处理程序。在reduce方法中,您正在访问 self.canvas 以调用draw方法。



您将创建此行上发生的canvas属性:

  self.canvas = FigCanvasTkAgg(fig,master = master)

设置 event_num 属性 App object;那么当您绑定它时,您不必将参数传递给处理程序。您可以通过在 __ init __ 中指定 self.event_num = 1 来实现。


I'm working on creating a program that utilizes Tkinter and matplotlib. I have 2 lists of lists (one for x-axis, one for y-axis) and I'm looking to have a button that can switch between the lists within the list. I took much of the code from the question Interactive plot based on Tkinter and matplotlib, but I can't quite get the button to work as I like. I'm quite new to using classes and having a bit of difficulty understanding them.

tft is the x-data tf1 is the y-data

Example of data:

x-data = [[1,2,3,4,5],[10,11,13,15,12,19],[20,25,27]]
y-data = [[5.4,6,10,11,6],[4,6,8,34,20,12],[45,25,50]]

My code below will graph one of the lists within a list, but won't switch between the lists within that list when I click the button. I've commented out other the methods I've tried. It always says that App has no attribute 'line' or 'canvas' when I use that. Like I said, I'm very new to classes and I'm trying to understand them better.

I've updated my code so that it now recognizes event_num and it prints the correct value whenever the button is pushed. However, the graph is not updating with the new data (i.e. it continues to only show the first data set instead of switching between lists). I believe the issue to be in the functions increase and decrease.I tried using the self.line, = ax.plot(tft[self.event_num],tf1[self.event_num],'.') and self.canvas.draw() but it's not working. I'm looking for that portion to be edited so that the graph will change.

class App: 


    def __init__(self, master):
        self.event_num = 1
        # Create a container
        frame = Frame(master)
        # Create 2 buttons
        self.button_left = Button(frame,text="< Previous Event",
                                        command=self.decrease)
        self.button_left.grid(row=0,column=0)
        self.button_right = Button(frame,text="Next Event >",
                                        command=self.increase)
        self.button_right.grid(row=0,column=1)

        fig = Figure()
        ax = fig.add_subplot(111)
        fig.autofmt_xdate()
        import matplotlib.dates as mdates
        ax.fmt_xdata = mdates.DateFormatter('%Y-%m-%d')
        self.line, = ax.plot(tft[self.event_num],tf1[self.event_num],'.')


        self.canvas = FigureCanvasTkAgg(fig,master=master)
        self.canvas.show()
        self.canvas.get_tk_widget().grid(row=1,column=0)
        frame.grid(row=0,column=0)

    def decrease(self):
        self.event_num -= 1
        print self.event_num
        self.line, = ax.plot(tft[self.event_num],tf1[self.event_num],'.')
        self.canvas.draw()
        #self.canvas.draw(tft[self.event_num],tf1[self.event_num],'.')
        #self.line.set_xdata(tft[event_num])
        #self.line.set_ydata(tf1[event_num])


    def increase(self):
        self.event_num += 1
        print self.event_num
        self.line, = ax.plot(tft[self.event_num],tf1[self.event_num],'.')
        self.canvas.draw()
        #self.canvas.draw(tft[self.event_num],tf1[self.event_num],'.')
        #self.set_xdata(tft[event_num])
        #self.set_ydata(tf1[event_num])


root = Tk()
app = App(root)
root.mainloop()

解决方案

The AttributeError: App instance has no attribute 'canvas' means that your code references the canvas attribute before it has been created/assigned.

This line:

self.button_left = Button(frame,text="< Previous Event",
                                    command=self.decrease(event_num))

is calling the decrease method because you used parentheses and provided arguments instead of just binding the handler. Inside the decrease method, you're accessing self.canvas to call the draw method.

That is happening before you create the canvas attribute, which happens on this line:

self.canvas = FigureCanvasTkAgg(fig,master=master)

Make event_num an attribute of the App object; then you won't have to pass arguments to the handler when you bind it. You can do this by assigning self.event_num = 1 inside __init__.

这篇关于使用matplotlib绘制Tkinter中的数据 - 在列表之间切换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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