2

我正在尝试构建一个小 GUI,它有一个每次读取新数据样本时都会更新的图。我不希望使用计时器运行它,因为数据将以不同的时间间隔到达。相反,我正在尝试使用信号进行实现,其中数据收集函数将在读取数据时发出信号,然后绘画函数将在绘画完成时发出信号。

现在出现的问题是,一旦我调用canvas.draw(),画布就没有更新。当这个程序运行时, data_collect() 和 paint() 交替发送信号,但是直到我停止该过程之后,数字才会更新。每当调用paint() 时,如何强制matplotlib 更新图形?

下面是一段相对简单的示例代码,它不是最佳的,但希望能传达我正在尝试做的事情的味道......

N_length = 150;
count = [0];

def sinval(delay):

    k = 0;
    x = [];

    # set up data vector with sinusoidal data in it.
    while k < N_length:  
        x.append(math.sin(2*math.pi*k/N_length));
        k += 1;

    def next():

        time.sleep(delay);
        outstring = "%0.3e" % (x[count[0]]);

        if (count[0] == (N_length-1)):
            count[0] = 0;
        else:
            count[0] += 1;

        return outstring;

    return next;


class DesignerMainWindow(QtGui.QMainWindow, Ui_mplMainWindow):

    def __init__(self, parent = None):
        super(DesignerMainWindow, self).__init__(parent)
        self.setupUi(self)

        QtCore.QObject.connect(self.mplStartButton, QtCore.SIGNAL("clicked()"), self.start_graph);
        QtCore.QObject.connect(self.mplStopButton, QtCore.SIGNAL("clicked()"), self.stop_graph);
        QtCore.QObject.connect(self.mplQuitButton, QtCore.SIGNAL("clicked()"), QtGui.qApp, QtCore.SLOT("quit()"));
        QtCore.QObject.connect(self, QtCore.SIGNAL("data_collect()"), self.data_collect);
        QtCore.QObject.connect(self, QtCore.SIGNAL("paint()"), self.paint);

    def start_graph(self):

        # generates first "empty" plots
        self.user = [];
        self.l_user, = self.mpl.canvas.ax.plot([], self.user, label='sine wave');

        # set up the axes. 
        self.mpl.canvas.ax.set_xlim(0, 300);
        self.mpl.canvas.ax.set_ylim(-1.1, 1.1);
        self.mpl.canvas.draw();

        # start the data collection process.
        self.delay = 0.05;
        self.next = sinval(self.delay);
        self.emit(QtCore.SIGNAL('data_collect()'));


    def data_collect(self):
        outstring = self.next();
        self.user.append(float(outstring.split()[0]));
        self.l_user.set_data(range(len(self.user)), self.user);
        self.emit(QtCore.SIGNAL('paint()'));


    def paint(self):
        self.mpl.canvas.draw();
        self.emit(QtCore.SIGNAL('data_collect()'));
4

1 回答 1

1

我猜想在paint() 之后调用 QCoreApplication::processEvents 会有所帮助。更优雅的是有一个单独的 QThread 用于阅读。看看这个线程

于 2010-08-18T17:13:16.203 回答