Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to draw crosshair and plot mouse position in pyqtgraph?

I am new to Python and pyqtgraph. I am working on a viewer for different kinds of signals. Currrently I got stuck when I want to include a crosshair and a text label with the mouse position. I am working with GridLayout, because later the graph is combined with several other elements.

I tried to adapt the pyqtgraph example Crosshair / mouse interaction but apart from many other things in pyqtgraph I do not understand the meaning of vb = signalgraph.vb just before mousemoved() and it the script raises a NameError

from pyqtgraph.Qt import QtGui, QtCore
import numpy as np
import pyqtgraph as pg

#QtGui.QApplication.setGraphicsSystem('raster')
app = QtGui.QApplication([])
mainwindow = QtGui.QMainWindow()
mainwindow.setWindowTitle('pyqtgraph example: PlotWidget')
mainwindow.resize(1000,800)
cw = QtGui.QWidget()
mainwindow.setCentralWidget(cw)

gridlayout = QtGui.QGridLayout()
cw.setLayout(gridlayout)

# define plot windows
signalgraph = pg.PlotWidget(name='Signalgraph')

# set position and size of plot windows
gridlayout.addWidget(signalgraph,0,0)

mainwindow.show()


# sample data
x = [0,1,2,3,4,5,6,7,8,9,10]
y = [0,0,0,8,8,8,9,9,9,0,0]

# plot 1
curve = pg.PlotCurveItem(x,y[:-1],pen='w',stepMode=True)
signalgraph.addItem(curve)

#cross hair in signalgraph
vLine = pg.InfiniteLine(angle=90, movable=False)
hLine = pg.InfiniteLine(angle=0, movable=False)
signalgraph.addItem(vLine, ignoreBounds=True)
signalgraph.addItem(hLine, ignoreBounds=True)

# Here I am not sure what to do ...
vb = signalgraph.vb
##vb = pg.ViewBox()


def mouseMoved(evt):
    pos = evt[0]
    if signalgraph.sceneBoundingRect().contains(pos):
        mousePoint = vb.mapSceneToView(pos)
        index = int(mousePoint.x())
        if index > 0 and index < len(x):
            label.setText("<span style='font-size: 12pt'>x=%0.1f, <span style='color: red'>y1=%0.1f</span>" % (mousePoint.x(), y[index], data2[index]))
        vLine.setPos(mousePoint.x())
        hLine.setPos(mousePoint.y())


proxy = pg.SignalProxy(signalgraph.scene().sigMouseMoved, rateLimit=60, slot=mouseMoved)
signalgraph.scene().sigMouseMoved.connect(mouseMoved)


# Start Qt event loop unless running in interactive mode or using pyside.
if __name__ == '__main__':
    import sys
    if (sys.flags.interactive != 1) or not hasattr(QtCore, 'PYQT_VERSION'):
        QtGui.QApplication.instance().exec_()

Thanks a lot Regards Michael

like image 939
Michael Avatar asked Jul 22 '13 08:07

Michael


1 Answers

"vb" is an attribute of the PlotItem class. Since signalgraph is a PlotWidget, you might not expect it to have the same attributes (although it does wrap some methods from its internal PlotItem). So the code you need is:

vb = signalgraph.plotItem.vb

If you are confused about the difference between PlotWidget and PlotItem, read up on the QGraphicsView and QGraphicsItem classes (both in the Qt documentation). PlotWidget is little more than a QGraphicsView with a PlotItem displayed inside.

like image 195
Luke Avatar answered Nov 01 '22 09:11

Luke