I have been trying to make a small tool to plot the mean of a region of a stack of images dynamically using matplotlib in Python. I have used the rectangle selector widget, similar to that used in Persistent rectangle selector
here is the code:
fig, (ax1,ax2) = plt.subplots(2)
ax1.imshow(im[:100].mean(axis=0))
line, =ax2.plot(im.mean(axis=1).mean(axis=1))
def line_select_callback(eclick, erelease):
global x1, y1 , x2, y2
'eclick and erelease are the press and release events'
x1, y1 = eclick.xdata, eclick.ydata
x2, y2 = erelease.xdata, erelease.ydata
print("(%3.2f, %3.2f) --> (%3.2f, %3.2f)" % (x1, y1, x2, y2))
print(" The button you used were: %s %s" % (eclick.button, erelease.button))
verts=np.array([x1, y1, x2, y2],np.uint16)
prof=im[:,verts[1]:verts[3],verts[0]:verts[2]].mean(axis=1).mean(axis=1)
ax2.plot(prof)
ax2.draw()
def toggle_selector(event):
global returned
print(' Key pressed.')
if event.key in ['Q', 'q'] and toggle_selector.RS.active:
print(' RectangleSelector deactivated.')
toggle_selector.RS.set_active(False)
if event.key in ['A', 'a'] and not toggle_selector.RS.active:
print(' RectangleSelector activated.')
toggle_selector.RS.set_active(True)
if event.key in ['Y', 'y'] and not toggle_selector.RS.active:
returned=True
plt.connect('key_press_event', toggle_selector)
toggle_selector.RS = RectangleSelector(ax1, line_select_callback,
drawtype='box', useblit=True,
button=[1, 3], # don't use middle button
minspanx=5, minspany=5,
spancoords='pixels',
interactive=True)
fig.show()
I have 2 issues with this: 1) the plot doesn't automatically update when i release my mouse drag 2) The rectangle selector tool seems to stick to my mouse even when I release my left mouse button.