2017-07-08 227 views
1

我目前正在处理一个数据集,其中包含持续时间约为10秒,采样时间为0.1秒的信号。 我的目标是提取这些数据的特定部分并将其保存到Python字典中。相关部分长约4秒。如何选择区域内的区域(Python)并提取区域内的数据

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

  1. 情节数据的整个10秒。

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

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

  4. 回到1.并采取新的数据。

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

预先感谢您和问候,

曼努埃尔

回答

3

你可以使用一个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

+0

惊人的 - 非常感谢您指出我在这个方向! Spanselector正是我所需要的,从未听说过它。 –