gpt4 book ai didi

python - 如何选择绘图中的区域 (Python) 并提取该区域内的数据

转载 作者:行者123 更新时间:2023-12-03 01:05:01 28 4
gpt4 key购买 nike

我目前正在处理一个数据集,其中包含持续时间约为 10 秒、采样时间为 0.1 秒的信号。我的目标是提取该数据的特定部分并将其保存到 python 字典中。相关部分长约4秒。

理想情况下,这就是我想做的:

  1. 绘制整个 10 秒的数据。

  2. 标记信号的相关部分,例如使用边界框。

  3. 关闭绘图窗口或按下按钮后提取边界框中的数据。

  4. 返回 1. 并获取新数据。

我看到 matplotlib 能够绘制补丁并提取补丁中的数据点。是否可以在创建绘图后(执行 plt.show() 命令后)向绘图添加补丁?

预先感谢您并致以最诚挚的问候,

曼努埃尔

最佳答案

您可以使用SpanSelector .

你基本上只需要添加一行来保存到 the matplotlib example .

import numpy as np
import matplotlib.pyplot as plt
from matplotlib.widgets import SpanSelector

fig = plt.figure(figsize=(8, 6))
ax = fig.add_subplot(211)

x = np.arange(0.0, 5.0, 0.01)
y = np.sin(2*np.pi*x) + 0.5*np.random.randn(len(x))

ax.plot(x, y, '-')
ax.set_ylim(-2, 2)
ax.set_title('Press left mouse button and drag to test')

ax2 = fig.add_subplot(212)
line2, = ax2.plot(x, y, '-')


def onselect(xmin, xmax):
indmin, indmax = np.searchsorted(x, (xmin, xmax))
indmax = min(len(x) - 1, indmax)

thisx = x[indmin:indmax]
thisy = y[indmin:indmax]
line2.set_data(thisx, thisy)
ax2.set_xlim(thisx[0], thisx[-1])
ax2.set_ylim(thisy.min(), thisy.max())
fig.canvas.draw_idle()

# save
np.savetxt("text.out", np.c_[thisx, thisy])

# set useblit True on gtkagg for enhanced performance
span = SpanSelector(ax, onselect, 'horizontal', useblit=True,
rectprops=dict(alpha=0.5, facecolor='red'))

plt.show()

enter image description here

关于python - 如何选择绘图中的区域 (Python) 并提取该区域内的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44990722/

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