【问题标题】:The value on plot is not updated图上的值未更新
【发布时间】:2016-11-28 13:37:57
【问题描述】:

绘图上的值不会更新。在我自己的课堂上,我从社区朋友那里整合了一个。绘图不会显示生成的值!我怎么能打开自动缩放?此外,我希望在程序执行期间移动 x 轴。例如,它总是显示最后 10 秒。

import Tkinter,time

root = Tkinter.Tk
class InterfaceApp(root):
    def __init__ (self, parent):
        root.__init__(self,parent)
        self.parent = parent
        self.initialize()
        self.on_stop()

    def initialize(self):
        self.min_x = 0
        self.max_x = 10
        self.xdata = []
        self.ydata = []
        self.on_launch()

    # Frame and LabelFrame create
        frInst = Tkinter.Frame(bg='', colormap='new')
        frBut = Tkinter.Frame(bg='', colormap='new')
        grInst = Tkinter.LabelFrame(frInst, text='Choose an Instruments', padx=5, pady=5)
        grSet = Tkinter.LabelFrame(frInst, text='Settings', padx=5, pady=7)

    # Create Settings and Buttons

        self.but_start = Tkinter.Button(frBut, text='Start', command=lambda: self.on_start(), width=10)
        self.but_stop = Tkinter.Button(frBut, text='Stop', command=lambda: self.on_stop(), width=10)
        self.but_quit = Tkinter.Button(frBut, text='Quit', command=lambda: self.on_quit(), width=10)

    # Grid
        frInst.grid(row=0)
        frBut.grid(row=3)
        grInst.grid(row=0)
        grSet.grid(row=0, column=1)

        # Power Supply Grid
        self.but_start.grid(row=2, column=1, sticky='W')
        self.but_stop.grid(row=2, column=2, sticky='W')
        self.but_quit.grid(row=2, column=3, sticky='W')


    # Function
    def on_quit(self):
        self.W_kSMU("output off")
        self.quit()
        self.destroy()

    def on_start(self):
        self.running = True
        self.but_start.configure(state='disabled')
        self.but_stop.configure(state='normal')
        self.Run()

    def Run(self):
        self.timer = randint(0,100)
        self.t_volt = randint(0,100)
        self.xdata.append(self.timer)
        self.ydata.append(self.t_volt)
        self.on_running(self.xdata, self.ydata)
        time.sleep(1)
        self.read_ps = 3

        if self.running:
            self.after(5000, self.Run)

    def on_launch(self):
        #Set up plot
        self.figure, self.ax = plt.subplots()
        self.lines, = self.ax.plot([],[], 'o')
        #Autoscale on unknown axis and known lims on the other
        self.ax.set_autoscaley_on(True)
        self.ax.set_xlim(self.min_x, self.max_x)
        #Other stuff
        self.ax.grid()

    def on_running(self, xdata, ydata):
        #Update data (with the new _and_ the old points)
        self.lines.set_xdata(xdata)
        self.lines.set_ydata(ydata)
        #Need both of these in order to rescale
        self.ax.relim()
        self.ax.autoscale_view()
        #We need to draw *and* flush
        self.figure.canvas.draw()
        self.figure.canvas.flush_events()

    def on_stop(self):
        self.but_start.configure(state='normal')
        self.but_stop.configure(state='disabled')
        self.running = False

if __name__ == "__main__":
    app = InterfaceApp(None)
    app.title("MPW")
    app.mainloop()

和我们朋友的代码:https://stackoverflow.com/a/15742183/6403009

【问题讨论】:

    标签: python class canvas plot tkinter


    【解决方案1】:

    我正在查看代码,“plt.subplots()”行是否正确?还有另一件事,在下面的行中,你写:“self.lines, =”也许逗号应该是关闭的,你能确认一下吗? 祝你好运!

    【讨论】:

    • 评论写在上面的评论部分,而不是答案部分。
    • @ParvizKarimli Ricardo 没有足够的声誉来发布 cmets。也就是说,我同意你的看法。这应该是评论,而不是答案。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-07-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多