我正在使用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)使用这段代码,我得到了这个图

但是我需要这样的东西

发布于 2019-12-12 06:48:46
根据librosa的说法,您可以为显示方法提供一个轴,以便在其上绘制项目,specshow,waveplot。我建议直接定义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,以获得类似的共享轴绘图。
发布于 2019-12-12 06:47:11
不使用子图,而是使用单个图的相同轴来显示这两个图。
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()https://stackoverflow.com/questions/59294748
复制相似问题