如何在gtk3中创建实时更新matplotlib图

时间:2013-03-31 22:42:38

标签: python matplotlib gtk3

我想把我的实时绘图应用程序放到一个不错的gtk3程序中,到目前为止,我得到了这个,但它没有显示窗口。我错过了什么?提前谢谢!

import serial
import numpy as np

from matplotlib.figure import Figure
from matplotlib.backends.backend_gtk3cairo import FigureCanvasGTK3Cairo as FigureCanvas
from gi.repository import Gtk

class MyWindow(Gtk.ApplicationWindow):
    def __init__(self):

    Gtk.Window.__init__(self, title="Toolbar Example")
        self.set_size_request(700, 500)
    self.box = Gtk.Box(spacing=6, orientation=Gtk.Orientation.VERTICAL)
    self.add(self.box)

    fig = Figure()
    ax = fig.add_subplot(111)
    ax.set_xlim(0, 50)
    ax.set_ylim(0, 20)

    ydata = [0] * 50
    line, = ax.plot(ydata, label='ydata')
    ax.legend()

    f = FigureCanvas(fig)
    self.box.pack_start(f, True, True, 0)

    while True:
        ydata.append(10)
        line.set_data(np.arange(len(ydata)), ydata)
        f.draw()


win = MyWindow()
win.connect("delete-event", Gtk.main_quit)
win.show_all()
Gtk.main()

1 个答案:

答案 0 :(得分:2)

使用cavas.draw()或 fig.canvas.draw()更新图表。

http://gtk3-matplotlib-cookbook.readthedocs.io/en/latest/enteringdata.html