gpt4 book ai didi

python - 将数据限制为所需的值

转载 作者:行者123 更新时间:2023-11-28 17:52:34 24 4
gpt4 key购买 nike

我有一个代码在上面左侧的图像中绘制线条。我想选择一个区域(上图右侧显示的矩形)并找出该区域内对应的线。

绘制左侧图像的代码是:

datfiles = glob.glob("*.dat")
for asc_filename in datfiles:
data = pylab.loadtxt(asc_filename)
x, y = data[:,0], data[:,3]
pylab.plot(x, y)

plt.legend()
pylab.savefig('ts.png')

您会为 Python 推荐哪些模块以及关于从何处开始编写代码的任何想法?

谢谢!

最佳答案

这不是太简单,但也不是很难。一般来说,你会想要为此使用某种完整的 gui 工具包(例如 Qt、wx、GTK、TK 等),但你可以使用“纯”matplotlib 来完成。 (当然,Matplotlib 只是使用您的安装配置为在幕后使用的任何 gui。)

您的第一个问题是您没有在任何地方存储轨道 ID。

它可以很简单:

import pylab
txt_files = glob.iglob("*.txt")
for txt_filename in txt_files:
data = pylab.loadtxt(txt_filename)

t, x, y = data[:,0], data[:,1], data[:,2]


line, = pylab.plot(x, y, label=time.ctime(t[0]))
line.track_id = filename[:-4]

但是,您必须将它们存储在某个地方!向每个 matplotlib 线条艺术家对象添加一个属性将使以后的事情变得更容易,但您可以通过其他方式轻松完成。

对于下一部分,matplotlib.widgets 中有一个小部件可以满足您的需要。看看这里的例子:http://matplotlib.sourceforge.net/examples/widgets/rectangle_selector.html同样,使用成熟的 gui 工具包更灵活,但 matplotlib 小部件可以很好地处理这样的事情。

最后,您需要了解如何确定您刚刚绘制的框是否与任何一条线重叠。我不会试图弄清楚顶点之间的线段是否穿过盒子,但找出点是否在盒子内部很简单,而且 matplotlib 已经有很多实用程序可以做到这一点。

把它们放在一个例子中:

import matplotlib.pyplot as plt
import numpy as np

from matplotlib.widgets import RectangleSelector
from matplotlib.transforms import Bbox

def main():
# Generate some random data:
data = []
for track_id in xrange(100):
a, b = np.random.random(2)
x = 100 * a + np.random.random(100).cumsum()
y = np.cos(x) + b * np.random.random(100).cumsum()
data.append((track_id, x, y))

# Plot it, keeping track of the "track_id"
fig, ax = plt.subplots()
for track_id, x, y in data:
line, = ax.plot(x,y)
line.track_id = track_id

# Make the selector...
selector = RectangleSelector(ax, onselect, drawtype='box')
# We could set up a button or keyboard shortcut to activate this, instead...
selector.set_active(True)

plt.show()

def onselect(eclick, erelease):
"""Get the lines in an axis with vertices inside the region selected.
"eclick" and "erelease" are matplotlib button_click and button_release
events, respectively."""
# Make a matplotlib.transforms.Bbox from the selected region so that we
# can more easily deal with finding if points are inside it, etc...
left, bottom = min(eclick.x, erelease.x), min(eclick.y, erelease.y)
right, top = max(eclick.x, erelease.x), max(eclick.y, erelease.y)
region = Bbox.from_extents(left, bottom, right, top)

track_ids = []
ax = eclick.inaxes
for line in ax.lines:
bbox = line.get_window_extent(eclick.canvas)
# Start with a rough overlaps...
if region.overlaps(bbox):
# We need the xy data to be in display coords...
xy = ax.transData.transform(line.get_xydata())

# Then make sure that at least one vertex is really inside...
if any(region.contains(x,y) for x,y in xy):
# Highlight the selected line by making it bold
line.set_linewidth(3)
track_ids.append(line.track_id)

print track_ids
eclick.canvas.draw()

if __name__ == '__main__':
main()

An example of some data being selected

关于python - 将数据限制为所需的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6906118/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com