如何在每次在 matplotlib 中单击鼠标时绘制一个点 [英] how to plot a dot each time at the point the mouse is clicked in matplotlib

查看:154
本文介绍了如何在每次在 matplotlib 中单击鼠标时绘制一个点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在 matplotlib(python 3) 中单击的点绘制点/圆?此外,我想读取每个点的像素值.我尝试绘制圆,但是每次读取不同的坐标值时,新圆都会覆盖前一个圆.谢谢

How is it possible to plot a dot/circle at the point being clicked on in matplotlib(python 3)? Moreover i want to read the pixel value of each dot. I have tried plotting the circles but each time i read a different coordinate value, the previous circle is overwritten by the new circle. thank you

推荐答案

你必须用图注册一个 onclick 事件.下面是一个在用户点击的地方绘制一个点的例子:

You have to register an onclick event with the figure. Below is an example that plots a dot where the user clicks:

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))
    plt.plot(event.xdata, event.ydata, ',')
    fig.canvas.draw()

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

这篇关于如何在每次在 matplotlib 中单击鼠标时绘制一个点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆