2017-07-08 700 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正是我所需要的,從未聽說過它。 –