Skip to main content
 首页 » 编程设计

python之如何以相同的比例绘制多个频谱图

2025年01月19日13arxive

我想绘制数据,然后以相同的比例从数据中减去噪声,这样我就可以准确地比较两者。最好的方法是什么? 现在我正在做:

f, t, Sxx = spectrogram(np.diff(rawdata), F_S) 
plt.subplot(211) 
plt.pcolormesh(t, f, Sxx) 
plt.ylabel('Frequency [Hz]') 
plt.xlabel('Time [sec]') 
plt.colorbar() 
 
f, t, Sxx = spectrogram(np.diff(rawdata - noise), F_S) 
plt.subplot(212) 
plt.pcolormesh(t, f, Sxx) 
plt.ylabel('Frequency [Hz]') 
plt.xlabel('Time [sec]') 
plt.colorbar() 

差异只是出于审美原因

请您参考如下方法:

您可以将 vmin 和 vmax 参数添加到对 pcolormesh 的调用中。将它们设置为两个数据集的最大值和最小值。 参见 https://matplotlib.org/api/_as_gen/matplotlib.axes.Axes.pcolormesh.html

f1, t1, Sxx1 = spectrogram(np.diff(rawdata), F_S) 
f2, t2, Sxx2 = spectrogram(np.diff(rawdata - noise), F_S) 
 
Sxx_min = np.amin(np.minimum(Sxx1, Sxx2)) 
Sxx_max = np.amax(np.maximum(Sxx1, Sxx2)) 
 
plt.subplot(211) 
plt.pcolormesh(t1, f1, Sxx1, vmin=Sxx_min, vmax=Sxx_max) 
plt.ylabel('Frequency [Hz]') 
plt.xlabel('Time [sec]') 
plt.colorbar() 
 
plt.subplot(212) 
plt.pcolormesh(t2, f2, Sxx2, vmin=Sxx_min, vmax=Sxx_max) 
plt.ylabel('Frequency [Hz]') 
plt.xlabel('Time [sec]') 
plt.colorbar()