我正在创建一个使用 Tkinter
和 matplotlib
的程序。我有 2 个列表列表(一个用于 x 轴,一个用于 y 轴),我希望有一个可以在列表中的列表之间切换的按钮。我从问题 Interactive plot based on Tkinter and matplotlib 中获取了大部分代码,但我无法完全让按钮按我喜欢的方式工作。我对使用类还很陌生,理解它们有点困难。
tft
是 x 数据
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]]
下面的代码将绘制列表中的一个列表,但当我单击按钮时不会在该列表中的列表之间切换。我已经注释掉了我尝试过的其他方法。当我使用它时,它总是说应用程序没有属性“line
”或“canvas
”。就像我说的,我对类(class)非常陌生,我正在努力更好地理解它们。
我已经更新了我的代码,现在它可以识别 event_num
并在按下按钮时打印正确的值。但是,图表并未使用新数据进行更新(即,它继续仅显示第一个数据集,而不是在列表之间切换)。我相信问题出在函数 increase
和 decrease
中。我尝试使用 self.line, = ax.plot(tft[self.event_num], tf1[self.event_num],'.')
和 self.canvas.draw()
但它不起作用。我正在寻找要编辑的部分,以便图表会发生变化。
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()
最佳答案
AttributeError: App 实例没有属性“canvas”
意味着您的代码在创建/分配 Canvas 属性之前引用了该属性。
这一行:
self.button_left = Button(frame,text="< Previous Event",
command=self.decrease(event_num))
正在调用 decrease
方法,因为您使用了括号并提供了参数,而不是仅仅绑定(bind)处理程序。在减少方法内,您将访问 self.canvas 来调用绘制方法。
这是在创建 Canvas 属性之前发生的,它发生在这一行:
self.canvas = FigureCanvasTkAgg(fig,master=master)
使event_num
成为App
对象的属性;那么在绑定(bind)处理程序时就不必将参数传递给处理程序。您可以通过在 __init__
内分配 self.event_num = 1
来完成此操作。
关于python - 使用 matplotlib 在 Tkinter 中绘制数据 - 在列表之间切换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33222030/