【问题标题】:Vertical line in an Interactive matplotlib plot with Tkinter使用 Tkinter 的交互式 matplotlib 图中的垂直线
【发布时间】:2013-09-30 09:11:08
【问题描述】:

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

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,但是当我最初将 x 和 y 坐标都赋予 plot() 时,事情似乎出了问题......

【问题讨论】:

  • “出错”是什么意思?
  • 对不起,我不是很清楚。我的意思是,当我只包含一个值列表(而不是明确地 x 和 y)时,事情显示得很好。我知道为什么现在会发生这种情况(见下文)。

标签: python matplotlib plot tkinter


【解决方案1】:

好吧,现在我很尴尬。这条线刚刚移出可见区域。我只需要更改增量值或使用set_xlim() 设置更大的可见窗口

哎呀!

【讨论】:

  • 很高兴听到您将其整理出来。请记住在允许时接受您自己的答案(我认为这是一个 2 天的等待期)。
猜你喜欢
  • 2021-01-07
  • 1970-01-01
  • 1970-01-01
  • 2012-04-17
  • 2017-02-05
  • 1970-01-01
  • 1970-01-01
  • 2018-01-07
  • 1970-01-01
相关资源
最近更新 更多