2013-02-03 36 views
1

我有一個散點圖,並已通過matplotlib http://matplotlib.org/users/transforms_tutorial.html#axes-coordinates實現了熒光筆類型的東西。onclick方法,捕獲座標突出顯示出現matplotlib

目前,當您點擊任何一個點時,熒光筆出現在相同的地方,但我想要做的是當您單擊某個點時,它將從該點開始座標並突出顯示點是。 我的代碼是這樣的

def onclick  
    ind = event.ind 
    # the x coords of this transformation are data, and the 
    # y coord are axes 
    trans = transforms.blended_transform_factory(
    ax.transData, ax.transAxes) 

    # highlight the 1..2 stddev region with a span. 
    # We want x to be in data coordinates and y to 
    # span from 0..1 in axes coords 
    rect = patches.Rectangle((1,0), width=1, height=1, 
        transform=trans, color='yellow', 
        alpha=0.5) 

    ax.add_patch(rect) 
    print ('on pick scatter:' , ind , np.take(x,ind), np.take(y,ind)) 

回答

1

有可能是一個更短的方式做到這一點,但是這是我的裂紋在它和它的作品:)

def onpick3(event): 
      ind = event.ind 
    l = np.take(x,ind) 
     for i in range(0,1): 
       d = l[i] 



    if highlight == 1 : 

     global highlight 
     highlight = 0; 
     self.canvas.draw() 
      if highlight == 0 : 

      trans = transforms.blended_transform_factory(self.axes.transData, self.axes.transAxes) 
       rect = patches.Rectangle(((d-10),0),width=25, height=25, transform=trans, color='yellow',alpha=0.5) 
       self.axes.add_patch(rect) 
      self.canvas.draw() 
     rect.remove() 
     else: 
     self.canvas.draw() 
     trans = transforms.blended_transform_factory(self.axes.transData, self.axes.transAxes) 
      rect = patches.Rectangle(((d-10),0),width=25, height=25, transform=trans, color='yellow',alpha=0.5) 
      self.axes.add_patch(rect) 
     self.canvas.draw() 
     highlight = 1 
     rect.remove() 
      print ('onpick3 scatter:', ind, np.take(x, ind), np.take(y, ind))