2017-03-09 147 views
2

我正在開發一個使用Python和PyQt4的應用程序,它可以繪製不同深度的參數。由於其良好的動畫速度特性,繪圖軟件包是PyQtGraph。由於我正在繪製深度,所以我想反轉Y軸。我發現我可以modify the ViewBox class in the PyQtGraph's documentation。所以我從我的Python站點包文件夾中修改了類的代碼。但我希望能夠從我的應用程序代碼中修改該類,而不必修改PyQtGraph的代碼(invertY=True)。原因是我想要一些帶有倒Y軸的PlotWidgets,其中一些沒有。有沒有什麼辦法可以做到這一點,例如在下面的代碼?我一直無法通過getting the ViewBox代碼做到這一點:在PyQtGraph中反轉Y軸

import sys 
from pyqtgraph.Qt import QtGui, QtCore 
import pyqtgraph as pg 
import random 
import time 

app = QtGui.QApplication([]) 
p = pg.plot() 
curve = p.plot() 

##initialize the arrays 
data = [] 
data.append(random.random()) 
n = [] 
n.append(time.clock()) 

##define the plotting function 
def update(): 

    data.append(data[-1] + 0.2 * (0.5 - random.random())) 
    n.append(time.clock()) 
    curve.setData(data,n) 
    time.sleep(0.1) 

timer = QtCore.QTimer() 
timer.timeout.connect(update) 
timer.start(0) 

if __name__ == '__main__': 

    if (sys.flags.interactive != 1) or not hasattr(QtCore, 'PYQT_VERSION'): 
     QtGui.QApplication.instance().exec_() 

回答

2

在你的情況,curvePlotDataItem。要獲取PlotDataItem的視圖框,請使用其getViewBox()方法。該視圖框然後有一個invertY方法。

p = pg.plot() 
curve = p.plot() 
curve.getViewBox().invertY(True)