Skip to content

Instantly share code, notes, and snippets.

@avaccari
Last active November 28, 2022 13:17
Show Gist options
  • Save avaccari/d8b7ca285dca30e8a87e60b7053c5607 to your computer and use it in GitHub Desktop.
Save avaccari/d8b7ca285dca30e8a87e60b7053c5607 to your computer and use it in GitHub Desktop.
A jupyter notebook showing how to display an interactive image and capture and return the position and other information about the mouse each time it is clicked over the image.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"%matplotlib notebook\n",
"import matplotlib.pyplot as plt\n",
"import numpy as np\n",
"import ipywidgets as wdg # Using the ipython notebook widgets\n",
"\n",
"# Create a random image\n",
"a = np.random.poisson(size=(12,15))\n",
"fig = plt.figure()\n",
"plt.imshow(a)\n",
"\n",
"# Create and display textarea widget\n",
"txt = wdg.Textarea(\n",
" value='',\n",
" placeholder='',\n",
" description='event:',\n",
" disabled=False\n",
")\n",
"display(txt)\n",
"\n",
"# Define a callback function that will update the textarea\n",
"def onclick(event):\n",
" txt.value = str(event) # Dynamically update the text box above\n",
" \n",
"# Create an hard reference to the callback not to be cleared by the garbage collector\n",
"ka = fig.canvas.mpl_connect('button_press_event', onclick)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.6"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment