使用Tkinter的交互式matplotlib图中的垂直线

时间:2013-09-24 23:18:08

标签: python matplotlib plot tkinter

我正在尝试以交互方式移动垂直线以可视化平分某些绘制数据,但我似乎无法显示调整后的线...

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

class App:
    def __init__(self, master, increment=.2, height=10):
        self.increment = increment
        # Create a container
        frame = Tkinter.Frame(master)
        # Make buttons...
        self.button_left = Tkinter.Button(frame,text="< Move Left",
                                        command=self.move_left)
        self.button_left.pack(side="left")
        self.button_right = Tkinter.Button(frame,text="Move Right >",
                                        command=self.move_right)
        self.button_right.pack(side="left")

        fig = Figure()
        ax = fig.add_subplot(111)
        x = [3]*height
        y = range(height)
        #so that it's a tuple
        self.line, = ax.plot(x, y)
        self.canvas = FigureCanvasTkAgg(fig, master=master)
        self.canvas.show()
        self.canvas.get_tk_widget().pack(side='top', fill='both', expand=1)
        frame.pack()

    def move_left(self):
        x, y = self.line.get_data()
        self.line.set_xdata(x-self.increment)
        x, y = self.line.get_data()
        print "x: {0}".format(x)
        print "y: {0}".format(y)
        #self.canvas.draw()
        self.canvas.blit()


    def move_right(self):
        x, y = self.line.get_data()
        self.line.set_xdata(x+self.increment)
        x, y = self.line.get_data()
        print "x: {0}".format(x)
        print "y: {0}".format(y)
        #self.canvas.draw()
        self.canvas.blit()

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

我查看了Joe Kingston的答案here,但是当我最初给plot()提供x和y坐标时,事情似乎出错......

1 个答案:

答案 0 :(得分:0)

好吧,现在我理所当然地感到很尴尬。这条线刚刚移出可见区域。我只需要更改增量值或使用set_xlim()

设置更大的可见窗口

糟糕!