Inverting the Y axis in PyQtGraph

Question:

I am developing an app with Python and PyQt4 that plots different parameters against depth. The plotting package is PyQtGraph because of its good animation speed features. Since I am plotting against depth, then I want to invert the Y axis. I have found that I can 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 in the code:

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_()
Asked By: Ivy

||

Answers:

In your case, curve is a PlotDataItem. To get the view box of a PlotDataItem use its getViewBox() method. The viewbox then has a invertY method.

p = pg.plot()
curve = p.plot()
curve.getViewBox().invertY(True)
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.