绘制音频波形和频谱图重叠

时间:2019-12-11 21:59:44

标签: python audio wave spectrogram

我正在使用librosa处理音频,并且需要在同一显示器中绘制频谱图和波形。

我的代码:

plt.figure(figsize=(14, 9))

plt.figure(1)

plt.subplot(211)
plt.title('Spectrogram')
librosa.display.specshow(stft_db, x_axis='time', y_axis='log')

plt.subplot(212)
plt.title('Audioform')
librosa.display.waveplot(y, sr=sr)

使用此代码,我得到了这个情节

但是我需要这样的东西

2 个答案:

答案 0 :(得分:0)

不是使用子图,而是使用单个图的相同轴来显示两个图形。

fig = plt.figure(figsize=(14, 9))
ax = librosa.display.specshow(stft_db, x_axis='time', y_axis='log')
librosa.display.waveplot(y, sr=sr, ax=ax)
plt.show()

答案 1 :(得分:0)

根据librosa,您可以为显示方法提供一个在specshowwaveplot上绘制项目的轴。我建议您完全定义您的matplotlib图形和子图,然后为librosa提供绘制它们的轴。

fig = plt.figure(figsize=(14, 9)) #This setups the figure
ax1 = fig.subplots() #Creates the Axes object to display one of the plots
ax2 = ax1.twinx() #Creates a second Axes object that shares the x-axis

librosa.display.specshow(stft_db, x_axis='time', y_axis='log', ax=ax1)
librosa.display.waveplot(y, sr=sr, ax=ax2)
plt.show()

为了获得所需的外观,可能还需要做更多的格式化,我建议您从matplotlib看一下this example,以获取类似的共享坐标轴图。

相关问题