python - How to draw crosshair and plot mouse position in pyqtgraph? -


i new python , pyqtgraph. working on viewer different kinds of signals. currrently got stuck when want include crosshair , text label mouse position. working gridlayout, because later graph combined several other elements.

i tried adapt pyqtgraph example crosshair / mouse interaction apart many other things in pyqtgraph not understand meaning of vb = signalgraph.vb before mousemoved() , script raises nameerror

from pyqtgraph.qt import qtgui, qtcore import numpy np import pyqtgraph 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 , 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 not sure ... 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 , 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 lot regards michael

"vb" attribute of plotitem class. since signalgraph plotwidget, might not expect have same attributes (although wrap methods internal plotitem). code need is:

vb = signalgraph.plotitem.vb 

if confused difference between plotwidget , plotitem, read on qgraphicsview , qgraphicsitem classes (both in qt documentation). plotwidget little more qgraphicsview plotitem displayed inside.


Comments

Popular posts from this blog

html5 - What is breaking my page when printing? -

html - Unable to style the color of bullets in a list -

c# - must be a non-abstract type with a public parameterless constructor in redis -