2012-08-12 133 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

希望這有助於你實現你的夢想。 :-)

相關問題