How to create a live updating matplotlib graph in gtk3

谁说胖子不能爱 提交于 2019-12-12 09:43:57

问题


I wanted to put my live plotting app into a nice gtk3 programm and so far I got this but it doesn't show a window. What am I missing? Thanks in advance!

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:


Use canvas.draw() or fig.canvas.draw() to update the plot.

See the matplotlib for more detail.



来源:https://stackoverflow.com/questions/15735426/how-to-create-a-live-updating-matplotlib-graph-in-gtk3

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!