0

I am trying to plot real-time data using matplotlib's scatter function (~200 fps) with a pyqt wrapper. Each frame consists of around 1000 points. However, I get a maximum of around 7 or 8 frames per second. My code is as follows:

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()

I've looked at several other sources online, but none of them have provided substantial answers that have helped me in my goal. Is there any way to speed up my code to reach ~250 fps? If not, are there any alternatives to matplotlib that will allow me to reach this speed?

Vedaad Shakib
  • 739
  • 7
  • 20
  • 2
    You can't _see_ that fast. mpl is not designed for that kind of speed (the cost of being flexible enough to output to a zoo of different formats/backends). Look at http://www.pyqtgraph.org/ or http://vispy.org/ – tacaswell May 20 '15 at 02:50
  • Using blitting will probably get you to 20-30 fps though – tacaswell May 20 '15 at 03:06
  • @tcaswell I've actually researched blitting. I was under the impression that blitting was only a viable solution for plot(), which returns a line object that is easily cached, not for scatter(), the function that I am currently using. – Vedaad Shakib May 20 '15 at 03:20
  • @tcaswell Btw, that was exactly what I was looking for. I would accept your answer if you posted it as an answer. – Vedaad Shakib May 20 '15 at 03:23
  • blitting has nothing to do with plot, it works with any artist. – tacaswell May 20 '15 at 03:35
  • @tcaswell Oh, I don't know matplotlib as well as I thought. Could you provide an example of blitting with scatter? All of the examples that I've found online are with plot... – Vedaad Shakib May 20 '15 at 03:45
  • See http://stackoverflow.com/questions/8955869/why-is-plotting-with-matplotlib-so-slow/8956211#8956211 and just relpace `line` with the object returned by scatter and change `set_data` -> (iirc) `set_offsets`, you will have to check the docs. I don't have time to write up an example tonight, sorry. – tacaswell May 20 '15 at 03:54
  • Curiously, the code you gave seems to be different from the code you're _actually_ using (e.g., see syntax error on line 67, and the undefined 'x' on line 69). – boardrider May 20 '15 at 13:09
  • Uhm..., line 67 is obviously a place holder for larger sections of code. – hitzg May 20 '15 at 15:51

0 Answers0