'Is there a way to speed up plotting and saving thousands of files to disk using matplotlib?

I'm extracting audio spectrogram features of 500+ audio files and saving them as .png files. It's taking way too much time per image.

Is there a way to speed up the plotting and saving process? I expect to have between 50-100 images per audio file.

Thus, a total of 25000+ images.

My code is below:

for audio_file in glob.glob(os.path.join(audio_path, '*trimmed.wav')):
  sample_rate, data = wavfile.read(audio_file)
  data = data/max(data)
  length = len(data)/sample_rate
  for i in range(0,(int((length))-1)):
    data_temp = data[i*sample_rate:(i+10)*sample_rate]
# Spectrogram of .wav file
    sample_freq, segment_time, spec_data = signal.spectrogram(data_temp, sample_rate)
    plt.xticks([]) #hide the x axis text
    plt.yticks([]) #hide the y axis text
    plt.pcolormesh(segment_time, sample_freq, np.log10(spec_data+1e-8))
    new_file_name = audio_file[:22] + str('de_') + audio_file[23:27] + str('_') + str(i) + '.png'
    plt.savefig(new_file_name, bbox_inches='tight')
    i=i+1


Solution 1:[1]

I found this to be faster than matplotlib's savefig. It uses moviepy that can be installed through conda install -c conda-forge moviepy :

from PIL import Image
from moviepy.video.io.bindings import mplfig_to_npimage
def savefig(fig, path):
    Image.fromarray(mplfig_to_npimage(fig)).save(path)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 endive1783