python Matplotlib gtk - 使用FuncAnimation制作动画图

时间:2015-06-14 11:09:37

标签: python animation matplotlib gtk

我正在尝试使用FunkAnimation更新GTK窗口中的绘图。 我想单击一个按钮开始更新从txt文件获取其数据的图。 txt文件不断更新。目的是绘制温度曲线。这是简化的代码:

import gtk
from matplotlib.backends.backend_gtkagg import FigureCanvasGTKAgg as FigureCanvas
import matplotlib.animation as animation
from matplotlib import pyplot as plt

class MyProgram():
    def __init__(self):
        some_gtk_stuff
        self.signals = {
                'on_button_clicked': self.create_plot,
        }
        self.builder.connect_signals(self.signals)

        self.vbox = self.builder.get_object('vbox')

        self.figure = plt.figure()
        self.axis = self.figure.add_subplot(1,1,1)
        self.init = 0

    def create_plot(self):                        
        def update_plot(i):
            #read SampleData from txt File
            x = []
            y = []
            readFile = open('SampleData.txt', 'r')
            sepFile = readFile.read().split('\n')
            readFile.close()

            for data in sepFile:
                xy = data.split(',')
                x.append(int(x)
                y.append(int(y)

            self.axis.plot(x, y)

            if (self.init == 0):
                self.canvas = FigureCanvas(self.figure)
                self.vbox.pack_start(self.canvas)
                self.canvas.show()

            ani = animation.FuncAnimation(self.figure, update_plot, interval = 1000)
            self.canvas.draw()
            return ani

MyProgram()
gtk.main()

所以我猜,问题是,create_plot函数只被调用一次。绘图窗口是在gui中创建的,但它不会更新。我无法找到解决问题的方法。如建议here添加“return any”不起作用。

您可以看到一个工作示例here,代码位于页面底部。

我知道我必须在以后实现类似日志记录和更新的线程,但我甚至没有让它工作。

任何提示? :)

1 个答案:

答案 0 :(得分:1)

我认为这是你想要实现的目标。请注意,我改变了您阅读文件的方式。 with open() as f:负责您忘记的文件关闭操作。也可以在构建器文件中写入信号处理程序的名称,这样就可以简单地说self.builder.connect(self)并省略self.signals dicitonary。

import gtk
from matplotlib.backends.backend_gtkagg import FigureCanvasGTKAgg as FigureCanvas
import matplotlib.animation as animation
from matplotlib import pyplot as plt

class MyProgram():
    def __init__(self):
        #some_gtk_stuff
        self.signals = {
                'on_button_clicked': self.create_plot,
        }
        self.builder.connect_signals(self.signals)

        self.vbox = self.builder.get_object('vbox')

        self.figure = plt.figure()
        self.axis = self.figure.add_subplot(1,1,1)
        self.canvas = None

    def create_plot(self, button):
        self.ani = animation.FuncAnimation(self.figure, self.update_plot, interval = 1000)


    def update_plot(self, i):
        #read SampleData from txt File
        x = []
        y = []
        with open('SampleData.txt') as f:
            x_raw, y_raw = f.readline().strip().split(',')
            x.append(int(x_raw))
            y.append(int(y_raw))

        self.axis.plot(x, y)

        if not self.canvas:
            self.canvas = FigureCanvas(self.figure)
            self.vbox.pack_start(self.canvas)
            self.canvas.show()

        self.canvas.draw()

MyProgram()
gtk.main()