matplotlib - Python: Return coordinate info on mouse click -
i display image in python , allow user click on specific pixel. want use x , y coordinates perform further calculations.
so far, i've been using event picker:
def onpick1(event): artist = event.artist if isinstance(artist, axesimage): mouseevent = event.mouseevent x = mouseevent.xdata y = mouseevent.ydata print x,y xaxis = frame.shape[1] yaxis = frame.shape[0] fig = plt.figure(figsize=(6,9)) ax = fig.add_subplot(111) line, = [ax.imshow(frame[::-1,:], cmap='jet', extent=(0,xaxis,0,yaxis), picker=5)] fig.canvas.mpl_connect('pick_event', onpick1) plt.show()
now function onpick1()
return x , y
can use after plt.show()
perform further calculations.
any suggestions?
a lesson gui programming go object oriented. problem right have asynchronous callback , want keep values. should consider packing together, like:
class myclickableimage(object): def __init__(self,frame): self.x = none self.y = none self.frame = frame self.fig = plt.figure(figsize=(6,9)) self.ax = self.fig.add_subplot(111) xaxis = self.frame.shape[1] yaxis = self.frame.shape[0] self.im = ax.imshow(self.frame[::-1,:], cmap='jet', extent=(0,xaxis,0,yaxis), picker=5) self.fig.canvas.mpl_connect('pick_event', self.onpick1) plt.show() # other associated methods go here... def onpick1(self,event): artist = event.artist if isinstance(artist, axesimage): mouseevent = event.mouseevent self.x = mouseevent.xdata self.y = mouseevent.ydata
now when click on point, set x
, y
attributes of class. however, if want perform calculations using x
, y
have onpick1
method perform calculations.
Comments
Post a Comment