1

I am creating spectrograms from audio files, but in several sections, I am getting white background. I would prefer the white sections to be transparent. For instance my current spectrogram : Current spectrogram

My desired spectrogram: Desired spectrogram

My current code for .wav files to spectrogram

def spectrogram_from_wav():
results = ['Positive', 'Negative', 'Unknown']
for res in results: 
    pathlib.Path(f"spectrograms/{res}").mkdir(parents=True, exist_ok=True)
    for files in tqdm(os.listdir(f"./cleaned_data/{res}")):
        filename = f"cleaned_data/{res}/{files}"
        #print(filename)
        x, sr = librosa.load(filename, mono=True)
        plt.specgram(x, NFFT=2048, Fs=2, Fc=0, noverlap=128, cmap='inferno', sides='default', mode='default', scale='dB');
        plt.axis('off');
        #plt.savefig(f"./spectrograms/{res}/{files[:-4]}.png")
        plt.clf()
        plt.show()
Mr. T
  • 8,325
  • 9
  • 23
  • 44
Blacky_99
  • 109
  • 1
  • 10

1 Answers1

2

Notice that the savefig has transparent option:

https://matplotlib.org/3.1.1/api/_as_gen/matplotlib.pyplot.savefig.html

How to export plots from matplotlib with transparent background?

Roman Pavelka
  • 692
  • 2
  • 12