从另一个类更新matplotlib图

时间:2014-04-27 01:55:10

标签: matplotlib tkinter figure

如何在tkinter中的另一个类中更新matplotlib图形作为帧的子图形?这是我被困住的地方。

class A():
    def__init__(self, master):
        sframe = Frame(master)
        sframe.pack(side=RIGHT)
        f = Figure(figsize=(3,2), dpi=100)

        a = f.add_subplot(122);
        # initially plots a sine wave 
        t = arange(0.0, 1, 0.01);
        s = sin(2*pi*t);
        a.plot(t,s);

        canvas = FigureCanvasTkAgg(f, master=sframe)
        canvas.show()
        canvas.get_tk_widget().pack(side=TOP, fill=BOTH, expand=1)
        # now i create the other object that will call show()
        data_obj = B(sframe)


class B():
    ...
    show(self, frame):
       _wlist = frame.winfo_children()
       for item in _wlist:
           item.clear() # or something like this
       # and then re-plot something or update the current plot

1 个答案:

答案 0 :(得分:0)

您必须以某种方式从show方法中检索您的图形对象f

  • 将f传递给B构造函数
  • class B:
        def __init__(self, frame, figure):
            self.figure = figure
        ...
        def show(self, frame):
            ...
            self.figure.plot( something )
    

  • 将f添加为框架的属性
  • class A:
        def__init__(self, master):
            ...
            sframe.fig = f
            data_obj = B(sframe)
    class B:
        def show(self, frame):
            ...
            frame.fig.plot( something )