From f8efbec53f10ec0dd5240528616037cc3aa40a31 Mon Sep 17 00:00:00 2001 From: Luke Campagnola Date: Sat, 7 Jul 2018 00:10:15 -0700 Subject: [PATCH] Add image hover callback to imageAnalysis example --- examples/imageAnalysis.py | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/examples/imageAnalysis.py b/examples/imageAnalysis.py index 13adf5ac..da753e34 100644 --- a/examples/imageAnalysis.py +++ b/examples/imageAnalysis.py @@ -21,7 +21,7 @@ win = pg.GraphicsLayoutWidget() win.setWindowTitle('pyqtgraph example: Image Analysis') # A plot area (ViewBox + axes) for displaying the image -p1 = win.addPlot() +p1 = win.addPlot(title="") # Item for displaying image data img = pg.ImageItem() @@ -93,6 +93,26 @@ def 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. if __name__ == '__main__':