2017-12-18 4 views
0

내 플롯에서 마우스 클릭으로 활성화되는 주석이 거의 없습니다. 하나의 특정 주석을 업데이트하고 싶습니다. 그러나 주석은 이전 주석보다 우선합니다. 이전 특정/특정 주석을 지우고 새 값으로 업데이트하여 깨끗하게 보이게하려면 어떻게해야합니까?matplotlib의 특정 주석 업데이트

from matplotlib import pyplot as plt 

fig, ax = plt.subplots() 
x=1 

def annotate(): 
    global x  
    if x==1:   
     x=-1 
    else: 
     x=1 
    ax.annotate(x, (0.5,0.5), textcoords='data', size=10) 
    ax.annotate('Other annotation', (0.5,0.4), textcoords='data', size=10) 

def onclick(event):  
    annotate() 
    fig.canvas.draw() 

cid = fig.canvas.mpl_connect('button_press_event',onclick) 

답변

1

annotation() 함수의 일부로 주석 객체를 생성 한 다음 업데이트 할 수 있습니다. 이 작업은 주석 객체의 텍스트 클래스 set_text() 메서드를 사용하여 수행 할 수 있습니다. (matplotlib.text.Annotation 클래스가 matplotlib.text.Text 클래스를 기반으로하기 때문에)

다음

는이 작업을 수행하는 방법입니다

from matplotlib import pyplot as plt 

fig, ax = plt.subplots() 
x=1 
annotation = ax.annotate('', (0.5,0.5), textcoords='data', size=10) # empty annotate object 
other_annotation = ax.annotate('Other annotation', (0.5,0.4), textcoords='data', size=10) # other annotate 

def annotate(): 
    global x 
    if x==1: 
     x=-1 
    else: 
     x=1 
    annotation.set_text(x) 


def onclick(event): 
    annotate() 
    fig.canvas.draw() 

cid = fig.canvas.mpl_connect('button_press_event',onclick) 
plt.show() 
+0

내가 정확하게 무엇을 찾고 있었다 즉. 감사. –