I am trying to make a simple user interface where the user selects some pixel coordinates in an image. I was thinking to do it using matplotlib, and thus I came across this stack overflow question: Store mouse click event coordinates with matplotlib
Where a solution is given that stores clicked coordinates in a global list
import numpy as np
import matplotlib.pyplot as plt
x = np.arange(-10,10)
y = x**2
fig = plt.figure()
ax = fig.add_subplot(111)
ax.plot(x,y)
coords = []
def onclick(event):
global ix, iy
ix, iy = event.xdata, event.ydata
print 'x = %d, y = %d'%(
ix, iy)
global coords
coords.append((ix, iy))
if len(coords) == 2:
fig.canvas.mpl_disconnect(cid)
return coords
cid = fig.canvas.mpl_connect('button_press_event', onclick)
The solution works just fine, however I would like to get rid of those global variables, and I am thinking that getting clicked coordinates would be a perfect job for asyncio.
Naively I tried following code, which obviously doesn't work (however it shows the general idea of what I wish to achieve):
import asyncio
import numpy as np
import matplotlib.pyplot as plt
queue = asyncio.Queue()
fig = plt.figure()
ax = fig.add_subplot(111)
ax.plot(np.random.rand(10))
@asyncio.coroutine
def onclick(event):
yield from queue.put(event.x)
print('button=%d, x=%d, y=%d, xdata=%f, ydata=%f' % (
event.button, event.x, event.y, event.xdata, event.ydata))
cid = fig.canvas.mpl_connect('button_press_event', onclick)
@asyncio.coroutine
def consume():
while True:
value = yield from queue.get()
print("Consumed", value)
loop = asyncio.get_event_loop()
loop.create_task(plt.show())
loop.create_task(consume())
loop.run_forever()
How can I utilize matplotlib and asyncio together to react to or collect events?