2017-10-26 20 views
0

저는 pyQtGraph를 사용하여 실시간 그래프 (주식 거래의 진화에 대한 그래프)를 플로팅하고 예제를 확인하는 데 해결할 수 없었던 몇 가지 질문이 있습니다.pyQtGraph 위젯 범위를 감지하고 스크롤 시작

  1. 왼쪽에서 오른쪽으로 페인팅을 시작하고 싶습니다. (기본값은 어떻게됩니까?) 경계 상자의 오른쪽에 도달하면 크기를 조정하지 않고 모든 새 데이터를 적합하게 만듭니다. 새로운 데이터는 오른쪽에서 입력하고 오래된 데이터는 왼쪽에서 사라집니다.

  2. numpy 배열에 데이터를 추가하면 배열의 새 인스턴스가 만들어집니다. 나는 이것을 원하지 않는다. pyQtGraph 플롯에 numpy 배열의 범위에서 데이터를 얻으려는 방법이 있습니까? 예를 들어 처음에는 10000 개의 부동 소수점 배열을 인스턴스화하고 pyQtGraph에 처음 100 개 부동 소수점을 표시하도록 지시 할 수 있습니까?

  3. 반면에 나는 배열을 제자리에서 수정하고 스크롤을 시뮬레이트하기 위해 숫자를 이동할 수 있다는 것을 알게되었습니다. pyQtGraph를 numpy 배열을 링으로 사용하는 방법이 있습니까? 난 단지 필요이 방법은 그래프 인덱스에서 시작되었는지 확인하는 모든 것이 ... 등 할당없이 일하는 것이 여기

내가 지금까지 가지고있는 코드, 매우 간단

:

class Grapher(QtWidgets.QMainWindow, Ui_MainWindow): 
    def __init__(self): 
     QtWidgets.QMainWindow.__init__(self) 
     Ui_MainWindow.__init__(self) 
     self.setupUi(self) 

     self.graphicsView.setTitle('My Graph') 
     self.graphicsView.setLabel('bottom', 'X axis') 
     self.graphicsView.setLabel('left', 'Y axis') 
     self.graphicsView.setLimits(xMin=0, yMin=0) 
     self.graphicsView.showGrid(x=True, y=True) 
     self.x=np.array(); 
     self.y=np.array(); 

     self._timer=QtCore.QTimer() 
     self._timer.timeout.connect(self.update) 
     self._timer.start(1000) 
     self._timerCounter=0; 

    def update(self): 
     self.x=np.append(self.x, [self._timerCounter]); 
     self.y=np.append(self.y, [math.sin(self._timerCounter)]) 
     self.graphicsView.plot(self.x, self.y) 
     self._timerCounter+=1; 

미리 감사드립니다.

+0

코드를 표시하십시오 !!!! – eyllanesc

답변

0

이렇게하면됩니다. 예를 들어, 1000 점의 배열이 있지만 200 점만 플롯하고자하는 경우 :

class Grapher(QtWidgets.QMainWindow, Ui_MainWindow): 

    def __init__(self): 
     QtWidgets.QMainWindow.__init__(self) 
     Ui_MainWindow.__init__(self) 
     self.setupUi(self) 

     self.graphicsView.setTitle('My Graph') 
     self.graphicsView.setLabel('bottom', 'X axis') 
     self.graphicsView.setLabel('left', 'Y axis') 
     self.graphicsView.setLimits(xMin=0, yMin=0) 
     self.graphicsView.showGrid(x=True, y=True) 
     self.plot = self.graphicsView.plot() 

     self.ptr = 0 
     self.x = np.zeros(1000) 
     self.y = np.zeros(1000) 
     self.xPlot = np.zeros(200) 
     self.yPlot = np.zeros(200) 

     self._timer = QtCore.QTimer() 
     self._timer.timeout.connect(self.update) 
     self._timer.start(1000) 
     self._timerCounter = 0 

    def update(self): 

     xNew = self._timerCounter 
     yNew = math.sin(xNew) 
     self.y[self.ptr] = math.sin(self._timerCounter) 
     self.x[self.ptr] = self._timerCounter 

     if self.ptr < 200: 
      # Normal assignment 
      self.yPlot[self.ptr] = self.y[self.ptr] 
      self.xPlot[self.ptr] = self.x[self.ptr] 
      self.plot.setData(self.xPlot[1:self.ptr + 1], 
           self.yPlot[1:self.ptr + 1]) 

     else: 
      # Shift arrays and then assign 
      self.yPlot[:-1] = self.yPlot[1:] 
      self.yPlot[-1] = self.y[self.ptr] 
      self.xPlot[:-1] = self.xPlot[1:] 
      self.xPlot[-1] = self.x[self.ptr] 
      self.plot.setData(self.xPlot, self.yPlot) 

     self.ptr += 1 
+0

그건 그렇고, self.xPlot [1 : self.ptr + 1]은 새로운 배열의 인스턴스를 생성하지 않습니까? – Notbad

+0

나는 정말로 모른다. 나는 이것이 단지 self.xplot의 견해라고 생각하지만 numpy의 내부 동작에 대한 적절한 지식이 부족하다. – Federico