我试图根据鼠标点击绘制一个圆形图。我已经编写了一个代码但是没有点击一个圆圈。
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)
试试这个:
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)
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)