1

我想绘制一首歌曲的频谱图,用作卷积神经网络图像分类的特征。因此,输出图像必须尽可能干净,没有任何标签/轴/刻度等。

从各种来源,我设法通过设置方法来禁用matplotlib默认绘制的box_inches=tight边框plt.savefig。尽管如此,我尝试绘制的任何图像上都会保留一个小边框。

此示例代码不使用实际的音频文件,但仍然可以看到小边框:

import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
import librosa
import librosa.display
import numpy as np

plt.axis('off')
S = np.array([[1,2,3], [2,2,1], [3,1,3]])
librosa.display.specshow(S)  # i suspect this method to somehow draw the border
plt.margins(0)  # as suggested by Eran W
plt.savefig('test.png', transparent=False, bbox_inches='tight', pad_inches=0)
plt.close()

带有我不喜欢的白色小边框的情节

我在该方法的官方文档中找不到任何内容,也不知道如何调试此问题。有什么提示吗?

4

2 回答 2

0

matplotlib您可以通过调用设置边距:

plt.margins(0)

文档:https ://matplotlib.org/devdocs/api/_as_gen/matplotlib.pyplot.margins.html

于 2018-01-31T05:15:34.690 回答
0

我有同样的问题,我找到了这个示例代码:

def melgram_v1(audio_file_path, to_file):
   sig, fs = librosa.load(audio_file_path)

   plt.axis('off')  # no axis
   plt.axes([0., 0., 1., 1.], frameon=False, xticks=[], yticks=[])  # Remove the white edge
   S = librosa.feature.melspectrogram(y=sig, sr=fs)
   librosa.display.specshow(librosa.power_to_db(S, ref=np.max))
   plt.savefig(to_file, bbox_inches=None, pad_inches=0)
   plt.close()
于 2021-04-30T20:41:40.520 回答