通过使用matplotlib单击绘制圆

问题描述 投票:0回答:2

我试图根据鼠标点击绘制一个圆形图。我已经编写了一个代码但是没有点击一个圆圈。

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)
python matplotlib canvas
2个回答
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
投票
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)
© www.soinside.com 2019 - 2024. All rights reserved.