使用 matplotlib 存储鼠标点击事件坐标
- 2025-03-05 09:18:00
- admin 原创
- 4
问题描述:
我正在尝试在 matplotlib 中实现一个简单的鼠标单击事件。我希望绘制一个图形,然后使用鼠标选择积分的下限和上限。到目前为止,我能够将坐标打印到屏幕上,但不能将其存储以供以后在程序中使用。我还想在第二次鼠标单击后退出与图形的连接。
下面是当前绘制并打印坐标的代码。
我的问题:
我如何将图形中的坐标存储到列表中?即 click = [xpos, ypos]
是否可以获得两组 x 坐标以便对该段线进行简单的积分?
import numpy as np
import matplotlib.pyplot as plt
x = np.arange(-10,10)
y = x**2
fig = plt.figure()
ax = fig.add_subplot(111)
ax.plot(x,y)
def onclick(event):
global ix, iy
ix, iy = event.xdata, event.ydata
print 'x = %d, y = %d'%(
ix, iy)
global coords
coords = [ix, iy]
return coords
for i in xrange(0,1):
cid = fig.canvas.mpl_connect('button_press_event', onclick)
plt.show()
解决方案 1:
mpl_connect 只需调用一次即可将事件连接到事件处理程序。它将开始监听点击事件,直到您断开连接。您可以使用
fig.canvas.mpl_disconnect(cid)
断开事件挂钩。
你想要做的事情是:
import numpy as np
import matplotlib.pyplot as plt
x = np.arange(-10,10)
y = x**2
fig = plt.figure()
ax = fig.add_subplot(111)
ax.plot(x,y)
coords = []
def onclick(event):
global ix, iy
ix, iy = event.xdata, event.ydata
print (f'x = {ix}, y = {iy}')
global coords
coords.append((ix, iy))
if len(coords) == 2:
fig.canvas.mpl_disconnect(cid)
return coords
cid = fig.canvas.mpl_connect('button_press_event', onclick)
解决方案 2:
我想在这里提供一个不同的答案,因为我最近尝试进行事件处理,但这里的解决方案没有区分缩放、平移和单击,在我的情况下一切都变得一团糟。我发现了一个名为mpl_point_clicker的 matplotlib 扩展,它对我来说非常有效,可以用 pip 安装(使用 python 3.X)。以下是他们文档中的基本用法:
import numpy as np
import matplotlib.pyplot as plt
from mpl_point_clicker import clicker
fig, ax = plt.subplots(constrained_layout=True)
ax.plot(np.sin(np.arange(200)/(5*np.pi)))
klicker = clicker(ax, ["event"], markers=["x"])
plt.show()
print(klicker.get_positions())
点击 3 次后输出如下
输出:
{'event': array([[ 24.22415481, 1.00237796],
[ 74.19892948, -0.99140661],
[123.23078387, 1.00237796]])}
解决方案 3:
感谢 otterb 提供的答案!我添加了一个小函数,取自这里...
在 numpy 数组中查找最近的值
所有这些代码将绘制图形,等待选择 x 点,然后返回任何积分、求和等所需的 x 数组的索引。
哒,
import numpy as np
import matplotlib.pyplot as plt
from scipy.integrate import trapz
def find_nearest(array,value):
idx = (np.abs(array-value)).argmin()
return array[idx]
# Simple mouse click function to store coordinates
def onclick(event):
global ix, iy
ix, iy = event.xdata, event.ydata
# print 'x = %d, y = %d'%(
# ix, iy)
# assign global variable to access outside of function
global coords
coords.append((ix, iy))
# Disconnect after 2 clicks
if len(coords) == 2:
fig.canvas.mpl_disconnect(cid)
plt.close(1)
return
x = np.arange(-10,10)
y = x**2
fig = plt.figure(1)
ax = fig.add_subplot(111)
ax.plot(x,y)
coords = []
# Call click func
cid = fig.canvas.mpl_connect('button_press_event', onclick)
plt.show(1)
# limits for integration
ch1 = np.where(x == (find_nearest(x, coords[0][0])))
ch2 = np.where(x == (find_nearest(x, coords[1][0])))
# Calculate integral
y_int = trapz(y[ch1[0][0]:ch2[0][0]], x = x[ch1[0][0]:ch2[0][0]])
print ''
print 'Integral between '+str(coords[0][0])+ ' & ' +str(coords[1][0])
print y_int
相关推荐
热门文章
项目管理软件有哪些?
- 2025年20款好用的项目管理软件推荐,项目管理提效的20个工具和技巧
- 2024年开源项目管理软件有哪些?推荐5款好用的项目管理工具
- 2024年常用的项目管理软件有哪些?推荐这10款国内外好用的项目管理工具
- 项目管理软件有哪些?推荐7款超好用的项目管理工具
- 项目管理软件有哪些最好用?推荐6款好用的项目管理工具
- 项目管理软件哪个最好用?盘点推荐5款好用的项目管理工具
- 项目管理软件排行榜:2024年项目经理必备5款开源项目管理软件汇总
- 项目管理必备:盘点2024年13款好用的项目管理软件
- 项目管理软件有哪些,盘点推荐国内外超好用的7款项目管理工具
- 2024项目管理软件排行榜(10类常用的项目管理工具全推荐)
热门标签
云禅道AD