2013-02-25 04:09:03 +00:00
|
|
|
"""
|
|
|
|
Demonstrates some customized mouse interaction by drawing a crosshair that follows
|
|
|
|
the mouse.
|
|
|
|
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
2012-04-03 05:11:39 +00:00
|
|
|
import initExample ## Add path to library (just for examples; you do not need this)
|
|
|
|
import numpy as np
|
|
|
|
import pyqtgraph as pg
|
|
|
|
from pyqtgraph.Qt import QtGui, QtCore
|
|
|
|
from pyqtgraph.Point import Point
|
|
|
|
|
2013-02-25 04:09:03 +00:00
|
|
|
#generate layout
|
2012-04-03 05:11:39 +00:00
|
|
|
app = QtGui.QApplication([])
|
|
|
|
win = pg.GraphicsWindow()
|
2013-02-25 04:09:03 +00:00
|
|
|
win.setWindowTitle('pyqtgraph example: crosshair')
|
2012-04-03 05:11:39 +00:00
|
|
|
label = pg.LabelItem(justify='right')
|
|
|
|
win.addItem(label)
|
|
|
|
p1 = win.addPlot(row=1, col=0)
|
|
|
|
p2 = win.addPlot(row=2, col=0)
|
2012-10-10 00:40:48 +00:00
|
|
|
|
2012-04-03 05:11:39 +00:00
|
|
|
region = pg.LinearRegionItem()
|
|
|
|
region.setZValue(10)
|
2013-11-05 03:07:43 +00:00
|
|
|
# Add the LinearRegionItem to the ViewBox, but tell the ViewBox to exclude this
|
|
|
|
# item when doing auto-range calculations.
|
|
|
|
p2.addItem(region, ignoreBounds=True)
|
2012-04-03 05:11:39 +00:00
|
|
|
|
2012-10-10 00:40:48 +00:00
|
|
|
#pg.dbg()
|
|
|
|
p1.setAutoVisible(y=True)
|
|
|
|
|
|
|
|
|
2012-04-03 05:11:39 +00:00
|
|
|
#create numpy arrays
|
|
|
|
#make the numbers large to show that the xrange shows data from 10000 to all the way 0
|
2014-03-11 23:01:34 +00:00
|
|
|
data1 = 10000 + 15000 * pg.gaussianFilter(np.random.random(size=10000), 10) + 3000 * np.random.random(size=10000)
|
|
|
|
data2 = 15000 + 15000 * pg.gaussianFilter(np.random.random(size=10000), 10) + 3000 * np.random.random(size=10000)
|
2012-04-03 05:11:39 +00:00
|
|
|
|
|
|
|
p1.plot(data1, pen="r")
|
|
|
|
p1.plot(data2, pen="g")
|
2012-10-10 00:40:48 +00:00
|
|
|
|
2012-04-03 05:11:39 +00:00
|
|
|
p2.plot(data1, pen="w")
|
|
|
|
|
|
|
|
def update():
|
2012-10-10 00:40:48 +00:00
|
|
|
region.setZValue(10)
|
|
|
|
minX, maxX = region.getRegion()
|
|
|
|
p1.setXRange(minX, maxX, padding=0)
|
|
|
|
|
2012-04-03 05:11:39 +00:00
|
|
|
region.sigRegionChanged.connect(update)
|
2012-10-10 00:40:48 +00:00
|
|
|
|
|
|
|
def updateRegion(window, viewRange):
|
|
|
|
rgn = viewRange[0]
|
|
|
|
region.setRegion(rgn)
|
|
|
|
|
|
|
|
p1.sigRangeChanged.connect(updateRegion)
|
|
|
|
|
2012-04-03 05:11:39 +00:00
|
|
|
region.setRegion([1000, 2000])
|
|
|
|
|
|
|
|
#cross hair
|
|
|
|
vLine = pg.InfiniteLine(angle=90, movable=False)
|
|
|
|
hLine = pg.InfiniteLine(angle=0, movable=False)
|
|
|
|
p1.addItem(vLine, ignoreBounds=True)
|
|
|
|
p1.addItem(hLine, ignoreBounds=True)
|
2012-10-10 00:40:48 +00:00
|
|
|
|
|
|
|
|
2012-04-03 05:11:39 +00:00
|
|
|
vb = p1.vb
|
|
|
|
|
|
|
|
def mouseMoved(evt):
|
|
|
|
pos = evt[0] ## using signal proxy turns original arguments into a tuple
|
|
|
|
if p1.sceneBoundingRect().contains(pos):
|
|
|
|
mousePoint = vb.mapSceneToView(pos)
|
|
|
|
index = int(mousePoint.x())
|
|
|
|
if index > 0 and index < len(data1):
|
|
|
|
label.setText("<span style='font-size: 12pt'>x=%0.1f, <span style='color: red'>y1=%0.1f</span>, <span style='color: green'>y2=%0.1f</span>" % (mousePoint.x(), data1[index], data2[index]))
|
|
|
|
vLine.setPos(mousePoint.x())
|
|
|
|
hLine.setPos(mousePoint.y())
|
2012-10-10 00:40:48 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
2012-04-03 05:11:39 +00:00
|
|
|
proxy = pg.SignalProxy(p1.scene().sigMouseMoved, rateLimit=60, slot=mouseMoved)
|
|
|
|
#p1.scene().sigMouseMoved.connect(mouseMoved)
|
|
|
|
|
|
|
|
|
|
|
|
## Start Qt event loop unless running in interactive mode or using pyside.
|
2012-12-05 05:25:45 +00:00
|
|
|
if __name__ == '__main__':
|
|
|
|
import sys
|
|
|
|
if (sys.flags.interactive != 1) or not hasattr(QtCore, 'PYQT_VERSION'):
|
|
|
|
QtGui.QApplication.instance().exec_()
|