Note
Go to the end to download the full example code.
Image click event#
Example showing how to use a click event on an image.

# test_example = false
import fastplotlib as fpl
import pygfx
import imageio.v3 as iio
data = iio.imread("imageio:camera.png")
# Create a figure
figure = fpl.Figure(size=(700, 560))
# create image graphic
image_graphic = figure[0, 0].add_image(data=data)
# show the plot
figure.show()
# adding a click event, we can also use decorators to add event handlers
@image_graphic.add_event_handler("click")
def click_event(ev: pygfx.PointerEvent):
# get the click location in screen coordinates
xy = (ev.x, ev.y)
# map the screen coordinates to world coordinates
xy = figure[0, 0].map_screen_to_world(xy)[:-1]
# print the click location
print(xy)
# NOTE: `if __name__ == "__main__"` is NOT how to use fastplotlib interactively
# please see our docs for using fastplotlib interactively in ipython and jupyter
if __name__ == "__main__":
print(__doc__)
fpl.loop.run()
Total running time of the script: (0 minutes 0.527 seconds)