2017-12-17 10 views
0

내가을 클릭 플롯 마우스 기반의 음모 원에 노력하고 있습니다를 사용하여 클릭하여 원을 그리기. 코드를 작성했지만 클릭시 원을 그리지 않습니다.는하기 matplotlib

from matplotlib import pyplot as plt 

fig, ax = plt.subplots() 
ax.set_xlim([-40, 40]) 
ax.set_ylim([-40, 40]) 
ax.set_aspect('equal') 

def onclick(event): 
    if event.dblclick: 
     circle=plt.Circle((event.xdata,event.ydata),2.5,color='black') 
     ax.add_patch(circle) 
cid = fig.canvas.mpl_connect('button_press_event',onclick) 

답변

0

이 시도 : 대답에 대한

from matplotlib import pyplot as plt 

def onclick(event): 
    if event.dblclick: 
     fig, ax = plt.subplots() 
     circle = plt.Circle((event.xdata,event.ydata),2.5,color='black') 
     ax.set_xlim([-40, 40]) 
     ax.set_ylim([-40, 40]) 
     ax.set_aspect('equal') 
     ax.add_artist(circle) 
     plt.show() 

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

감사합니다. 또는 함수의 마지막 줄로 추가하면 문제가 해결됩니다. fig.canvas.draw() –

+0

아무런 문제 없습니다. 그렇습니다.'fig.canvas.draw()'또는'plt.show()'는 모두 효과가있었습니다. – Ivan86

0
from matplotlib import pyplot as plt 

fig, ax = plt.subplots() 
ax.set_xlim([-40, 40]) 
ax.set_ylim([-40, 40]) 
ax.set_aspect('equal') 

def onclick(event): 
    if event.dblclick: 
     circle=plt.Circle((event.xdata,event.ydata),2.5,color='black') 
     ax.add_patch(circle) 
     fig.canvas.draw() #this line was missing earlier 
cid = fig.canvas.mpl_connect('button_press_event',onclick)