当我单击两点时,是否有方法绘制特定值?

2024-04-23 11:49:43 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在做一个项目,当你点击两个点时,它将绘制我设置的x和y值。我是matplotlib的新手,所以我不知道这是否可行

    import matplotlib.pyplot as plt

    fig = plt.figure()
    ax = fig.add_subplot(111)
    ax.set_xlim([0, 10])
    ax.set_ylim([0, 10])

    def onclick(event):
        print('button=%d, x=%d, y=%d, xdata=%f, ydata=%f' %
        (event.button, event.x, event.y, event.xdata, event.ydata))
        if event.x == 215 and event.x == 420:
           plt.plot([2,5],[3,3])

    plt.plot(event.xdata, event.ydata, '-ro')
    fig.canvas.draw()

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

我希望每次单击两个点时,设置为“绘图”的值都会出现


Tags: 项目eventplotmatplotlibfig绘制buttonplt