2017-05-21 184 views
1

目标是在图像上单击我的鼠标两次并检索2个坐标。代码如下:Matplotlib捕获错误的鼠标坐标

class ImageListener(object): 

def __init__(self): 
    self.coordinates = [] 

def onclick(self, event): 
    print(event.x, event.y) 
    self.coordinates.extend([event.x, event.y]) 

def show_image(self, img, close=True): 
    fig = plt.figure() 
    ax = fig.add_subplot(111) 
    ax.imshow(img) 
    cid = fig.canvas.mpl_connect('button_press_event', self.onclick) 
    while len(self.coordinates) < 3: 
     plt.waitforbuttonpress(0) 
    if close: 
     plt.close(fig) 

    fig.canvas.mpl_disconnect(cid) 

但是,捕获的坐标是错误的。在下图中,当我点击时,我的鼠标位于(700,333)处。 (鼠标在屏幕截图中消失)。相反,(514,154)被捕获。

enter image description here

缺少什么我在这里?

回答

0

的事情是,你正在使用event.xevent.y

  • X:X位置 - 像素从左边的画布

  • Y:y位置 - 像素的画布的底部

如果你想要鼠标在数据坐标系中的坐标,你应该使用:

  • XDATA:在数据COORDS鼠标的X坐标
  • YDATA:在数据COORDS小鼠Y的坐标

参见doc