Merge pull request #719 from campagnola/image-hover-example

Add image hover callback to imageAnalysis example
This commit is contained in:
Luke Campagnola 2018-07-07 12:17:11 -07:00 committed by GitHub
commit 809704ac45
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -21,7 +21,7 @@ win = pg.GraphicsLayoutWidget()
win.setWindowTitle('pyqtgraph example: Image Analysis') win.setWindowTitle('pyqtgraph example: Image Analysis')
# A plot area (ViewBox + axes) for displaying the image # A plot area (ViewBox + axes) for displaying the image
p1 = win.addPlot() p1 = win.addPlot(title="")
# Item for displaying image data # Item for displaying image data
img = pg.ImageItem() img = pg.ImageItem()
@ -93,6 +93,26 @@ def updateIsocurve():
isoLine.sigDragged.connect(updateIsocurve) isoLine.sigDragged.connect(updateIsocurve)
def imageHoverEvent(event):
"""Show the position, pixel, and value under the mouse cursor.
"""
if event.isExit():
p1.setTitle("")
return
pos = event.pos()
i, j = pos.y(), pos.x()
i = int(np.clip(i, 0, data.shape[0] - 1))
j = int(np.clip(j, 0, data.shape[1] - 1))
val = data[i, j]
ppos = img.mapToParent(pos)
x, y = ppos.x(), ppos.y()
p1.setTitle("pos: (%0.1f, %0.1f) pixel: (%d, %d) value: %g" % (x, y, i, j, val))
# Monkey-patch the image to use our custom hover function.
# This is generally discouraged (you should subclass ImageItem instead),
# but it works for a very simple use like this.
img.hoverEvent = imageHoverEvent
## Start Qt event loop unless running in interactive mode or using pyside. ## Start Qt event loop unless running in interactive mode or using pyside.
if __name__ == '__main__': if __name__ == '__main__':