2013-08-26 21 views
1

使用iPython和matplotlib,我希望能夠添加註釋(或任何對象),從圖中刪除它,然後重新添加它。基本上我想切換圖中對象的外觀。在matplotlib |中刪除並重新添加對象切換對象外觀matplotlib

這是我如何添加和刪除此對象。該對象在remove()之後仍然存在。但我無法弄清楚如何讓它重新出現在圖表中。

an = ax.annotate('TEST', xy=(x, y), xytext=(x + 15, y), arrowprops=dict(facecolor='#404040')) 
draw() 
an.remove() 

回答

1

你想set_visibledoc

an = gca().annotate('TEST', xy=(.1, .1), xytext=(.1 + 15,.1), arrowprops=dict(facecolor='#404040')) 
gca().set_xlim([0, 30]) 
draw() 
plt.pause(5) 
an.set_visible(False) 
draw() 
plt.pause(5) 
an.set_visible(True) 
draw() 
+0

這是完美的。謝謝! – DJElbow

+0

是的,但問題的關鍵不是'set_visible',而是'draw' :) –

2

an.remove()幫助片斷雲: 「直到這個數字是重繪的影響將是不可見的」。如果你這樣做:

import numpy as np 
import matplotlib.pyplot as plt 

fig = plt.figure('A figure title') 
ax = fig.add_subplot(111, autoscale_on=False, xlim=(-1,5), ylim=(-3,5)) 

t = np.arange(0.0, 5.0, 0.01) 
s = np.cos(2*np.pi*t) 
line, = ax.plot(t, s, lw=3, color='purple') 

ann=ax.annotate('offset', xy=(1, 1), xycoords='data',xytext=(-15, 10),  textcoords='offset points',arrowprops=dict(facecolor='black', shrink=0.05),horizontalalignment='right', verticalalignment='bottom') 

它會畫一個帶註釋的圖。要刪除它,所有你需要做的是:

ann.remove() 
fig.canvas.draw() 

所以你所缺少的是重繪數字。

相關問題