从图中选择n个数据点 [英] Select n data points from plot
本文介绍了从图中选择n个数据点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想通过在图中单击om来选择点并将其存储在数组中.我想在n个选择之后停止选择点,例如通过按一个键.我怎样才能做到这一点?这就是我到目前为止所拥有的.
I want to select points by clicking om them in a plot and store the point in an array. I want to stop selecting points after n selections, by for example pressing a key. How can I do this? This is what I have so far.
import numpy as np
import matplotlib.pyplot as plt
fig = plt.figure()
ax = fig.add_subplot(111)
ax.set_title('click on points')
line, = ax.plot(np.random.rand(100), 'o', picker=5) # 5 points tolerance
def onpick(event):
thisline = event.artist
xdata = thisline.get_xdata()
ydata = thisline.get_ydata()
ind = event.ind
points = tuple(zip(xdata[ind], ydata[ind]))
print('onpick points:', points)
fig.canvas.mpl_connect('pick_event', onpick)
plt.show()
推荐答案
要具有GUI功能,您必须将绘图嵌入GUI框架中;但是,有一种简单的方法可以限制所选项目的数量:
To have GUI functionality, you will have to embed the plot in a GUI frame; however, there is a simple way to limit the number of selected items:
import matplotlib
matplotlib.use('TkAgg')
import numpy as np
import matplotlib.pyplot as plt
fig = plt.figure()
ax = fig.add_subplot(111)
ax.set_title('click on points')
line, = ax.plot(np.random.rand(100), 'o', picker=5) # 5 points tolerance
points = []
n = 5
def onpick(event):
if len(points) < n:
thisline = event.artist
xdata = thisline.get_xdata()
ydata = thisline.get_ydata()
ind = event.ind
point = tuple(zip(xdata[ind], ydata[ind]))
points.append(point)
print('onpick point:', point)
else:
print('already have {} points'.format(len(points)))
fig.canvas.mpl_connect('pick_event', onpick)
plt.show()
示例输出:
onpick point: ((54.0, 0.68482963903475647),)
onpick point: ((54.0, 0.68482963903475647),)
onpick point: ((54.0, 0.68482963903475647),)
onpick point: ((54.0, 0.68482963903475647),)
onpick point: ((54.0, 0.68482963903475647),)
already have 5 points
如果要选择唯一点,则可以使用集合来存储它们,而不是列表.
If you want to select unique points, you can use a set to store them instead of a list.
这篇关于从图中选择n个数据点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文