基于Tkinter和Matplotlib的交互式图表

5

亲爱的编程社区,

我正在尝试使用Tkinter和pylab.plot执行"交互式图绘制",以绘制1D值。横坐标是一个1D 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矩阵的元素),并使用左右两个按钮在它们之间进行切换(以便前往更复杂的情况)。通常,我会创建一些类似以下示例的函数来绘制图表。

import pylab
def graphic_plot(n):
    fig = pylab.figure(figsize=(8,5))
    pylab.plot(x,Y[n,:],'x',markersize=2)
    pylab.show()

为了添加两个按钮来改变n参数的值,我尝试了以下方法但没有成功:
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()

能否有人帮助我了解如何执行这种功能?非常感谢。


你遇到了什么错误?你能详细介绍一下“我尝试过但没有成功”吗? - mgilson
首先,我无法将图形和按钮包含在一个窗口中。其次,我不知道如何通过按钮更改 n 参数的值。第三,我不知道如何在单击按钮后“刷新”图形。 - user1187727
我不知道其中一些,但要让画布显示出来,您需要将其打包。通过谷歌搜索,似乎您可能想要:canvas.get_tk_widget().pack(side=Tkinter.TOP, fill=Tkinter.BOTH, expand=1) -- 或者您想如何打包它。 (我通常使用.grid(),所以我不确定传递给.pack()的合理选项是什么)。 - mgilson
在http://stackoverflow.com/questions/33222030/plotting-data-in-tkinter-with-matplotlib-switching-between-lists和http://stackoverflow.com/questions/33262433/unable-to-update-tkinter-matplotlib-graph-with-buttons-and-custom-data中进一步讨论了此问题。 - Bennett Brown
1个回答

13

要更改线条的y值,请保存绘制时返回的对象(line,= ax.plot(...)),然后使用line.set_ydata(...)。 要重新绘制图表,请使用canvas.draw()

以下是基于你的代码的更完整示例:

import Tkinter
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
from matplotlib.figure import Figure

class App:
    def __init__(self, master):
        # Create a container
        frame = Tkinter.Frame(master)
        # Create 2 buttons
        self.button_left = Tkinter.Button(frame,text="< Decrease Slope",
                                        command=self.decrease)
        self.button_left.pack(side="left")
        self.button_right = Tkinter.Button(frame,text="Increase Slope >",
                                        command=self.increase)
        self.button_right.pack(side="left")

        fig = Figure()
        ax = fig.add_subplot(111)
        self.line, = ax.plot(range(10))

        self.canvas = FigureCanvasTkAgg(fig,master=master)
        self.canvas.show()
        self.canvas.get_tk_widget().pack(side='top', fill='both', expand=1)
        frame.pack()

    def decrease(self):
        x, y = self.line.get_data()
        self.line.set_ydata(y - 0.2 * x)
        self.canvas.draw()

    def increase(self):
        x, y = self.line.get_data()
        self.line.set_ydata(y + 0.2 * x)
        self.canvas.draw()

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

这几乎是我想要的。谢谢。你能解释一下这个语法 self.line, = (特别是逗号)吗?我失去了matplotlib工具(如缩放...),有办法把它们找回来吗? - user1187727
有没有可能拥有分离的窗口(一个带有pylab中经典绘图,另一个仅带有两个按钮),以便更轻松地进行自定义? - user1187727

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接