Question

I am using pyqtgraph for real-time data plotting. Unfortunately, the library does not support time series real-time plotting, so I went googling and found someone that had written specific classes for that (you can find it here). I used these classes once with no problem at all, but now python instantly crashes upon call of the 'TimeSeriesPlot()' class. I tried to delete all .pyc files in the python folder and all subfolders, but that did not change anything, neither did a repair of the python install. Any ideas as to what causes the problem and how to solve it? Has anyone been confronted to that sort of problem? My configuration is:

  • Windows 7 Home Edition Blockquote
  • Python 2.7.5
  • Pyside 1.1.2 with QT 4.8
  • PyQtGraph 0.9.7

Here is the content of the file I have been trying to execute:

from pyqtgraph.Qt import QtGui, QtCore
import numpy as np
import pyqtgraph as pg

class TimeSeriesPlotViewBox(pg.ViewBox):
    def __init__(self, timeSeriesPlot, *args, **kwds):
       pg.ViewBox.__init__(self, *args, **kwds)
       self.timeSeriesPlot = timeSeriesPlot


   def mouseClickEvent(self, ev):
      if ev.button() == QtCore.Qt.RightButton:

         self.timeSeriesPlot.xFrom = None
         self.timeSeriesPlot.xTo = None
         self.timeSeriesPlot.updateView()

         self.enableAutoRange(pg.ViewBox.XAxis,1.0)
         self.enableAutoRange(pg.ViewBox.YAxis,1.0)      


   def mouseDoubleClickEvent(self, ev):
      if ev.button() == QtCore.Qt.LeftButton:

         xFrom = 0
         if not self.timeSeriesPlot.xFrom is None and self.timeSeriesPlot.xFrom < 0:
            xFrom = self.timeSeriesPlot.xFrom 
         else:
            xFrom = -len(self.timeSeriesPlot.timedata)
         self.timeSeriesPlot.xFrom = int(round(xFrom / 2.0))


         self.timeSeriesPlot.updateView()
         self.enableAutoRange(pg.ViewBox.XAxis,1.0)
         self.enableAutoRange(pg.ViewBox.YAxis,1.0)    

   def mouseDragEvent(self, ev):
      if ev.button() == QtCore.Qt.RightButton:
         ev.ignore()
      else:
         pg.ViewBox.mouseDragEvent(self, ev)


class TimeSeriesPlot(pg.QtCore.QObject):

   def __init__(self, tsTitle, parent = None):
      pg.QtCore.QObject.__init__(self, parent)    

      self.vb =TimeSeriesPlotViewBox(self)

      self.plt = pg.PlotWidget(viewBox=self.vb, title = tsTitle)
      self.vb.sigRangeChangedManually.connect(self.zoom)

      #time axis
      self.timedata = []

      #val data
      self.valdata = []
      self.curveVal = pg.PlotDataItem([])
      self.plt.addItem(self.curveVal)

      self.xFrom = -50
      self.xTo = None

   def zoom(self):
       xlimits,ylimits = self.vb.viewRange()

       import bisect
       self.xFrom = bisect.bisect_left(self.timedata,xlimits[0])
       self.xTo = bisect.bisect_right(self.timedata,xlimits[1])

       self.vb.disableAutoRange(pg.ViewBox.XAxis)
       self.vb.disableAutoRange(pg.ViewBox.YAxis)

       self.updateView()

   def show(self):
       self.plt.show()

   def updateModel(self,newdata):
       time = float(newdata["time"])
       val = float(newdata["val"])

       self.timedata.append(time)
       self.valdata.append(val)

   def updateView(self):
       useAA = True
       viewSlice = None
       maxElementCnt = 500.0
       if self.xFrom is None and self.xTo is None:
          elementCnt = len(self.timedata)
          step = max(int(round(elementCnt / maxElementCnt)),1)
          viewSlice = slice(-elementCnt,None,step)
       elif self.xFrom < 0:
          elementCnt = -self.xFrom
          step = max(int(round(elementCnt / maxElementCnt)),1)
          viewSlice = slice(-elementCnt,None,step)
       else:
          elementCnt = self.xTo - self.xFrom
          step = max(int(round(elementCnt / maxElementCnt)),1)
          viewSlice = slice(self.xFrom,self.xTo,step)

       useAA = True    
       self.curveVal.setData(x=self.timedata[viewSlice],y=self.valdata[viewSlice],clear=True,antialias=useAA)
####################################
TimeSeriesPlot('plot')
Was it helpful?

Solution

The first error message I see when I run this code is:

QWidget: Must construct a QApplication before a QPaintDevice

This is true for all Qt applications; you must create a QApplication instance before anything else. After this is fixed, the script still crashes with a segmentation fault, probably because the TimeSeriesPlot instance is being deleted immediately (since it is not assigned to any variable). Even after that is fixed, the script exits immediately (because there is nothing else to do). I replaced the last line in your example with the following code to get the expected result:

app = pg.QtGui.QApplication([])
tsp = TimeSeriesPlot('plot')
tsp.show()
app.exec_()
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top