2012-08-12 135 views
1

我有一个散布图,通过imshow(地图)。 我想要一个点击事件来添加一个新的散点,我已经通过scater(newx,newy)完成)。麻烦的是,我希望添加使用pick事件删除点的功能。由于没有remove(pickX,PickY)函数,我必须得到拾取的索引并将它们从列表中移除,这意味着我不能像上面那样创建我的分散结构,我必须分散(allx,盟友)。matplotlib:重绘之前清除分散数据

所以底线是我需要一种方法去除散点图并用新数据重绘它,而不会改变我的imshow的存在。我试过并尝试过: 只是一次尝试。

fig = Figure() 
axes = fig.add_subplot(111) 
axes2 = fig.add_subplot(111) 
axes.imshow(map) 
axes2.scatter(allx,ally) 
# and the redraw 
fig.delaxes(axes2) 
axes2 = fig.add_subplot(111) 
axes2.scatter(NewscatterpointsX,NewscatterpointsY,picker=5) 
canvas.draw() 

令我惊喜,这省去了我的imshow和斧头太:(。 实现我的梦想的任何方法是非常赞赏。 安德鲁

回答

2

首先,你应该有一个在读好events docs here

你可以附加一个函数,每当鼠标点击的时候都会被调用,如果你保存了一个可以选择的艺术家列表(这里指的是点),那么你可以询问鼠标点击事件是否在艺术家,并呼吁艺术家的方法remove方法d。如果没有,你可以创建一个新的艺术家,并把它添加到点击点列表:

import matplotlib.pyplot as plt 

fig = plt.figure() 
ax = plt.axes() 

ax.set_xlim(0, 1) 
ax.set_ylim(0, 1) 

pickable_artists = [] 
pt, = ax.plot(0.5, 0.5, 'o') # 5 points tolerance 
pickable_artists.append(pt) 


def onclick(event): 
    if event.inaxes is not None and not hasattr(event, 'already_picked'): 
     ax = event.inaxes 

     remove = [artist for artist in pickable_artists if artist.contains(event)[0]] 

     if not remove: 
      # add a pt 
      x, y = ax.transData.inverted().transform_point([event.x, event.y]) 
      pt, = ax.plot(x, y, 'o', picker=5) 
      pickable_artists.append(pt) 
     else: 
      for artist in remove: 
       artist.remove() 
     plt.draw() 


fig.canvas.mpl_connect('button_release_event', onclick) 

plt.show() 

enter image description here

希望这有助于你实现你的梦想。 :-)

相关问题