Merge pull request #719 from campagnola/image-hover-example
Add image hover callback to imageAnalysis example
This commit is contained in:
commit
809704ac45
@ -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__':
|
||||
|
Loading…
Reference in New Issue
Block a user