0

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')
4
  • Please show your exception. Jun 4, 2013 at 19:45
  • There is no exception to show. Python crashes silently. All other things continue to run perfectly well.
    – Raoul
    Jun 4, 2013 at 20:00
  • Can you provide an example of how you are using the linked class? Also, please provide information about your OS and versions of pyqt and pyqtgraph.
    – Luke
    Jun 4, 2013 at 23:52
  • Here is my config. I just tried to call an empty instance. I have trouble with code block formatting on SO, but the code is correctly formatted on my computer.
    – Raoul
    Jun 5, 2013 at 8:11

1 Answer 1

1

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_()
1
  • Thanks! I expected to do things incorrectly, since I am a beginner with python and programming in general. Actually, I came up with this file because I had the classes working once, and when my crashing problems started I tried to trace my mistake by removing lines one at a time.
    – Raoul
    Jun 7, 2013 at 8:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.