【发布时间】:2012-04-17 09:05:40
【问题描述】:
亲爱的编程社区,
我正在尝试基于 Tkinter 和 pylab.plot 执行“交互式绘图”以绘制一维值。横坐标是一维 numpy 数组 x,纵坐标值在多维数组 Y 中,例如。
import numpy
x = numpy.arange(0.0,3.0,0.01)
y = numpy.sin(2*numpy.pi*x)
Y = numpy.vstack((y,y/2))
我想根据 x 显示 y 或 y/2(Y 矩阵的元素),并使用左右 2 个按钮在它们之间进行切换(以便处理更复杂的情况)。通常我会创建一些类似下面的函数来绘制图表。
import pylab
def graphic_plot(n):
fig = pylab.figure(figsize=(8,5))
pylab.plot(x,Y[n,:],'x',markersize=2)
pylab.show()
要添加两个按钮来改变nparameter的值,我试过这个没有成功:
import Tkinter
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
class App:
def __init__(self,master):
# Create a container
frame = Tkinter.Frame(master)
frame.pack()
# Create 2 buttons
self.button_left = Tkinter.Button(frame,text="<",command=self.decrease)
self.button_left.pack(side="left")
self.button_right = Tkinter.Button(frame,text=">",command=self.increase)
self.button_right.pack(side="left")
self.canvas = FigureCanvasTkAgg(fig,master=self)
self.canvas.show()
def decrease(self):
print "Decrease"
def increase(self):
print "Increase"
root = Tkinter.Tk()
app = App(root)
root.mainloop()
有人可以帮助我了解如何执行此类功能吗?非常感谢。
【问题讨论】:
-
您遇到什么错误?您能否详细说明“我已经尝试过但没有成功”?
-
首先,我不能将图形和按钮包含在一个窗口中。其次,我不知道如何使用按钮更改
n参数的值。第三,点击按钮后我不知道如何“刷新”图表。 -
我不知道其中的一些,但要让画布显示出来,你需要打包它。谷歌搜索了一下,看起来你可能想要:
canvas.get_tk_widget().pack(side=Tkinter.TOP, fill=Tkinter.BOTH, expand=1)——或者你想打包它。 (我通常使用 .grid(),所以我不确定传递给 .pack() 的合理选项是什么)
标签: python user-interface matplotlib tkinter