在pyqt包装器中加速matplotlib的分散功能

时间:2015-05-20 02:47:06

标签: python matplotlib plot pyqt

我正在尝试使用matplotlib的散射函数(~200 fps)和pyqt包装器绘制实时数据。每帧由大约1000个点组成。但是,我每秒最多可获得7或8帧。我的代码如下:

import sys
import random
import types
import re
import sys
import os
import matplotlib.pyplot as plt

from PyQt4                              import QtGui, QtCore
from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.backends.backend_qt4agg import NavigationToolbar2QTAgg as NavigationToolbar
from matplotlib                         import pyplot as plt
from time                               import sleep

class Window(QtGui.QDialog):
    def __init__(self, increment=10, nSteps=500, timestep=0.0004, parent=None):
        super(Window, self).__init__(parent)

        # frame increment
        self.increment = increment
        self.nSteps = nSteps
        self.timestep = timestep # in seconds

        # a figure instance to plot on
        self.figure = plt.figure()
        self.ax1 = self.figure.add_subplot(1, 1, 1)

        # this is the Canvas Widget that displays the `figure`
        # it takes the `figure` instance as a parameter to __init__
        self.canvas = FigureCanvas(self.figure)

        # this is the Navigation widget
        # it takes the Canvas widget and a parent
        self.toolbar = NavigationToolbar(self.canvas, self)

        # Just some button connected to `plot` method
        self.button = QtGui.QPushButton('Plot')
        self.button.clicked.connect(self.start)

        # the current frame
        self.index = 0

        # set the layout
        layout = QtGui.QVBoxLayout()
        layout.addWidget(self.toolbar)
        layout.addWidget(self.canvas)
        layout.addWidget(self.button)
        self.setLayout(layout)

    def start(self):
        # connects timer to dynamic plot
        self.timer = QtCore.QTimer(self)
        self.timer.timeout.connect(self.updatePlot)
        self.timer.start(100)

    def updatePlot(self):
        if self.index > self.increment*self.nSteps:
            self.timer.stop()
            return

        data = [["ghost_points.dat", "green"], ["wall_points.dat", "red"], [("mps.%s.out") % self.index, "blue"]]
        self.index += self.increment

        self.ax1.cla() # clear axes

        for i in data:
            <gets x and y data and color>

            self.ax1.scatter(x, y, c=color)

        self.canvas.draw()

def mpsPlot():
    app = QtGui.QApplication(sys.argv)

    main = Window()
    main.show()

    sys.exit(app.exec_())

if __name__ == "__main__":
    mpsPlot()

我已经在网上查看了其他几个来源,但没有一个提供了帮助我实现目标的实质性答案。有没有办法加快我的代码达到~250 fps?如果没有,是否有任何替代matplotlib可以让我达到这个速度?

0 个答案:

没有答案