在matplotlib中手动控制FuncAnimation的时间循环

5 投票
1 回答
502 浏览
提问于 2025-04-18 09:57

我在找一个类似于 FuncAnimation 的东西,带有 blit 功能。但是我不想让库在固定的时间间隔自动调用一个函数,而是想在我准备好的时候自己调用这个函数。我不太明白 matplotlib 是如何处理函数返回的坐标轴来更新它们的。我正在处理来自外部源的实时数据,想要让刷新频率和这些数据保持同步。

1 个回答

0

我大概是这样做的

import sys
import os
import random
from PySide import QtGui,QtCore
os.environ['QT_API'] = 'pyside'
from matplotlib import use
use('Qt4Agg')
import pylab as plt

class Example(QtGui.QMainWindow):

    def __init__(self):
        super(Example, self).__init__()
        self.setWindowTitle('Widgets')
        self.setGeometry(300, 300, 250, 150)

        self.wid = QtGui.QWidget()
        self.grid = QtGui.QGridLayout()
        self.wid.setLayout(self.grid)
        self.setCentralWidget(self.wid)

        self.dat = []
        self.timer = QtCore.QTimer()
        self.timer.timeout.connect(self.toc)
        self.timer.start(100)
        self.show()

        self.fig = plt.figure(13)
        plt.show()


    def toc(self):
        val = random.uniform(-1.7, 0.78)
        self.dat.append(val)
        plt.ion()
        fig = plt.figure(13)
        plt.clf()
        plt.plot(self.dat)
        plt.ioff()

def main():
    app = QtGui.QApplication(sys.argv)
    ex = Example()
    sys.exit(app.exec_())

if __name__ == '__main__':
    main()

撰写回答