Funcanimation不在figcanvas内部工作

时间:2018-02-06 13:59:31

标签: python matplotlib pygtk gtk3 glade

我想构建一个界面,其中包含一些显示某些实验结果的实时图表。为此,我决定使用glade(UI),gtk,python和matplotlib的组合。我正在处理一些基础知识,并且能够绘制一些实时图表。

现在,我在使用Funcanimation进行实时动画时遇到了一些麻烦。下面,代码导入一个带有四个滚动窗口的glade文件,我想在每个滚动窗口中显示一些动画。我没有在画布里面(在滚动窗口内)绘图而累了动画而且它有效!但是当我试图运行它时,Funcanimation(update_line)的回调函数甚至没有触发。实际上我在这里做错了什么。我也是python的新手。

由于

#!/usr/bin/env python
import sys
import os
import time
import psutil as p
import threading
import numpy as np
from gi.repository import Gtk
from gi.repository import GObject
from matplotlib.figure import Figure
import matplotlib.animation as animation
from matplotlib.backends.backend_gtk3cairo import FigureCanvasGTK3Cairo as FigureCanvas
import matplotlib.pyplot as plt




class windowSignals:
    def on_mainWindow_destroy(self, widget):
        Gtk.main_quit()

def main():
    builder = Gtk.Builder()
    builder.add_from_file("window.glade")
    builder.connect_signals(windowSignals())
    window = builder.get_object("mainWindow")
    sw = builder.get_object("scrolledWindow1")


    def update_line(num, data, line):
        data.pop(0)
        data.append(np.random.random())
        line.set_ydata(data)

        return line,


    fig1 = plt.figure()
    data = [0.0 for i in xrange(100)]
    l, = plt.plot(data, 'r-')

    plt.ylim(-1, 1)
    line_ani = animation.FuncAnimation(fig1, update_line, 25, fargs=(data, l), interval=50, blit=True)
    can = FigureCanvas(fig1)
    sw.add_with_viewport(can)
    can.draw()

    window.show_all()
    Gtk.main()

if __name__ == "__main__":
    main()

1 个答案:

答案 0 :(得分:1)

非常感谢@ImportanceOfBeingErnest deserves credit基本上这篇文章中的所有内容都是正确的:)

这是一个稍微修改过的可运行版本的代码(没有Glade),它在GTK应用程序中使用FuncAnimation。

需要注意三点:

  • A reference to the FuncAnimation object must be kept以免对象(及其计时器)进行垃圾回收。

  • FigureCanvas应在FuncAnimation之前创建,因为FuncAnimation会通过调用fig.canvas.new_timer()来创建计时器。如果尚未创建画布,fig.canvas为无,您将获得AttributeError

  • 如果Gtk不是您的默认后端,请在此处使用matplotlib.figure.Figure代替plt.figure

import numpy as np
from gi.repository import Gtk
from gi.repository import GObject
from matplotlib.figure import Figure
from matplotlib.backends.backend_gtk3cairo import FigureCanvasGTK3Cairo as FigureCanvas
import matplotlib.pyplot as plt
import matplotlib.animation as animation

class MyApp(object):
    def __init__(self):
        window = Gtk.Window()
        window.connect("delete-event", Gtk.main_quit)
        window.set_default_size(400, 400)
        sw = Gtk.ScrolledWindow()
        window.add(sw)

        self.fig = fig = Figure()
        self.canvas = FigureCanvas(fig)
        sw.add_with_viewport(self.canvas)

        ax = fig.add_subplot(111)
        data = np.zeros(100)
        self.line, = ax.plot(data, 'r-')
        ax.set_ylim(-1, 1)
        self.ani = animation.FuncAnimation(
            self.fig, self.update_line, interval=100, frames=50, repeat=True)

        window.show_all()

    def update_line(self, *args):
        data = 2*(np.random.random(100)-0.5)
        self.line.set_ydata(data)
        self.canvas.draw()
        return True

if __name__ == "__main__":
    app = MyApp()
    Gtk.main()