【发布时间】:2017-09-05 10:17:25
【问题描述】:
我已经查看了对这个问题的回复:How would I iterate over a list of files and plot them as subplots on a single figure?
但我对如何实现我的目标并不明智。我想将具有不同 x 轴的多个数据集绘制到 Python 中的单个图形上。我在下面包含了我的代码的 sn-p,它对数据集执行 FFT,然后计算 3 个 Butterworth 滤波器输出。理想情况下,我希望将所有内容都绘制在一个数字上,我试图在下面的代码中实现这一点。 for 循环计算 3 个 Butterworth 滤波器输出,上面的代码 - FFT 和下面的代码尝试将 FFT 曲线和 sqrt(0.5) 线附加到先前生成的图中以进行显示。
任何方向或建议将不胜感激。
"""Performs a Fast Fourier Transform on the data specified at the base of the code"""
def FFT(col):
x = io2.loc[1:,'Time']
y = io2.loc[1:,col]
# Number of samplepoints
#N = 600
N = pd.Series.count(x)
N2 = int(N/2)
# sample spacing
#T = 1.0 / 800.0
T = 1/(io2.loc[2,'Time'] - io2.loc[1,'Time'])
#x = np.linspace(0.0, N*T, N)
#y = np.sin(50.0 * 2.0*np.pi*x) + 0.5*np.sin(80.0 * 2.0*np.pi*x)
yf = scipy.fftpack.fft(y)
xf = np.linspace(0.0, 1.0/(2.0*T), N2)
fig=plt.figure()
plt.clf()
i=1
for order in [3, 6, 9]:
ax=fig.add_subplot(111, label="order = %d" % order)
b, a = butter_lowpass(cutoff, fs, order=order)
w, h = freqz(b, a, worN=2000)
ax.plot((fs * 0.5 / np.pi) * w, abs(h))
i=i+1
ax4=fig.add_subplot(111, label='sqrt(0.5)', frame_on=False)
ax5=fig.add_subplot(111, label="FFT of "+col, frame_on=False)
ax4.plot([0, 0.5 * fs], [np.sqrt(0.5), np.sqrt(0.5)], '--')
ax5.plot(xf, 2.0/N * np.abs(yf[:N2]))
plt.xlabel('Frequency (Hz)')
plt.ylabel('Gain')
plt.grid(True)
plt.legend(loc='best')
#fig, ax = plt.subplots()
#ax.plot(xf, 2.0/N * np.abs(yf[:N2]), label="FFT of "+col)
plt.axis([0,5000,0,0.1])
#plt.xlabel('Frequency (Hz)')
#plt.ylabel('Amplitude (mm)')
#plt.legend(loc=0)
plt.show()
return
亲切的问候,
【问题讨论】:
标签: python-3.x matplotlib plot