在 PyQtGraph 中反转 Y 轴
Inverting the Y axis in PyQtGraph
我正在使用 Python 和 PyQt4 开发一个应用程序,根据深度绘制不同的参数。绘图包是 PyQtGraph,因为它具有良好的动画速度特性。因为我是针对深度绘图的,所以我想反转 Y 轴。我发现我可以 modify the ViewBox class in the PyQtGraph's documentation. So I've modified the class's code from my Python Site Packages folder. But I would like to be able to modify the class from within my app code, and not having to modify PyQtGraph's code (invertY=True
). The reason is that I want some PlotWidgets with an inverted Y axis and some of them without. Is There any way I can do so for instance in the following code? I have not been able to do it by getting the ViewBox 在代码中:
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_()
在您的例子中,curve
是 PlotDataItem
。要获取 PlotDataItem
的视图框,请使用其 getViewBox()
方法。然后视图框有一个 invertY
方法。
p = pg.plot()
curve = p.plot()
curve.getViewBox().invertY(True)
我正在使用 Python 和 PyQt4 开发一个应用程序,根据深度绘制不同的参数。绘图包是 PyQtGraph,因为它具有良好的动画速度特性。因为我是针对深度绘图的,所以我想反转 Y 轴。我发现我可以 modify the ViewBox class in the PyQtGraph's documentation. So I've modified the class's code from my Python Site Packages folder. But I would like to be able to modify the class from within my app code, and not having to modify PyQtGraph's code (invertY=True
). The reason is that I want some PlotWidgets with an inverted Y axis and some of them without. Is There any way I can do so for instance in the following code? I have not been able to do it by getting the ViewBox 在代码中:
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_()
在您的例子中,curve
是 PlotDataItem
。要获取 PlotDataItem
的视图框,请使用其 getViewBox()
方法。然后视图框有一个 invertY
方法。
p = pg.plot()
curve = p.plot()
curve.getViewBox().invertY(True)