0
我正在嘗試使用matplotlib小部件SpanSelector來找出在某些用戶點擊的時間間隔的情節的頻率。我試圖用matplotlib小部件SpanSelector來做到這一點,但我不確定如何做到這一點。我嘗試修改給出的示例http://matplotlib.org/examples/widgets/span_selector.html ,但它不起作用,仍然只在選定區域中放大。這裏是我的代碼,我想一起工作:SpanSelector與傅立葉分析
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, axisbg='#FFFFCC')
def make_f(start,end):
sum = 0
for n in range(start,end+1):
sum += np.cos(n * np.pi *time)
return sum
time = np.linspace(0,10,2000)
signal = make_f(1,10)
ax.plot(time, signal, '-')
#ax.set_ylim(-2,2)
ax.set_title('Press left mouse button and drag to test')
fourier = np.fft.rfft(signal)
n = signal.size
rate = time[1]-time[0]
ax2 = fig.add_subplot(212, axisbg='#FFFFCC')
line2, = ax2.plot(np.fft.rfftfreq(n,rate), 20*np.log10(fourier), '-')
def onselect(xmin, xmax):
indmin, indmax = np.searchsorted(time, (xmin, xmax))
indmax = min(len(time)-1, indmax)
thisx = time[indmin:indmax]
thisy = signal[indmin:indmax]
line2.set_data(thisx, thisy)
ax2.set_xlim(thisx[0], thisx[-1])
ax2.set_ylim(thisy.min(), thisy.max())
fig.canvas.draw()
# set useblit True on gtkagg for enhanced performance
def make_fourier():
signal = SpanSelector(ax, onselect, 'horizontal', useblit=True,
rectprops=dict(alpha=0.5, facecolor='red'))
fourier = np.fft.rfft(signal)
n = signal.size
rate = time[1]-time[0]
return fourier
span = make_fourier()
plt.show()