2012-08-31 87 views
4

我在wxpython GUI中使用matplotlib。總之,我繪製了一堆數據。然後我點擊一個數據點(使用DataCursor發現在:。Python和刪除圖中的註釋

Is there a matplotlib equivalent of MATLAB's datacursormode?

Pop up annotations on matplotlib within wxPython

第二個環節是我的實際執行情況,我從其他類refrense的datacursor類什麼我想知道,如何通過單擊事件按鈕來刪除註釋?例如,我有一個事件按鈕來更新我的散點圖(通過使用plot_handle.set_data而不是通過清除圖)。但是,註釋仍然準確無論這點是否是th無論是否。我如何刪除它?

謝謝!

回答

6

大多數matplotlib對象有一個remove()函數(我認爲它是從Artist繼承)。只需在要刪除的對象上調用它即可。

編輯:

如果你有

dc = DataCursor(...) # what ever aruguements you give it 
# bunch of code 
dc.annotation.remove() 
del dc 
plt.draw() # need to redraw to make remove visible 

Python有沒有「私人」屬性,所以你可以達到你內心的對象,並調用卸下注釋概念。請參閱tutorial on classes for more details

這樣做的缺點是,現在你有一個對象是一個奇怪的狀態。如果您有任何其他參考,它們可能表現不好。如果你想保持DataCursor對象周圍,你可以使用它的set_*功能修改annotation對象或改變它的知名度暫時隱藏它(doc

+0

但我如何確定對象的名稱?這是我似乎遇到麻煩,因爲它是在另一個類(另一個「自我」 - 如果這是有道理的?) – mcfly

+0

@mcfly我會建議(重新)閱讀http://docs.python.org/ tutorial/classes.html。 – tacaswell

+0

謝謝!!!效果很好 – mcfly

1

您需要添加它像一個藝術家來刪除它像一個藝術家

arrow2 = matplotlib.text.Annotation("I love it",xy=(0.5,0.5),xycoords='data',arrowprops=dict(arrowstyle="-")) 
ax2.add_artist(arrow2) 
arrow2.remove()