Save an image (only content, without axes or anything else) to a file using Matloptlib

前端 未结 2 1680
梦谈多话
梦谈多话 2021-01-01 06:25

I\'d like to obtain a spectrogram out of a wav file and then save it to a png, but I need just the content of the image (not axes or anything else). I came across these ques

2条回答
  •  暖寄归人
    2021-01-01 07:17

    I think you want subplots_adjust:

    fig,ax = plt.subplots(1)
    fig.subplots_adjust(left=0,right=1,bottom=0,top=1)
    ax.axis('tight')
    ax.axis('off')
    

    In this case:

    import matplotlib.pyplot as plt
    from scipy.io import wavfile
    import numpy as np
    
    def graph_spectrogram(wav_file):
        rate, data = wavfile.read(wav_file)
        fig,ax = plt.subplots(1)
        fig.subplots_adjust(left=0,right=1,bottom=0,top=1)
        ax.axis('off')
        pxx, freqs, bins, im = ax.specgram(x=data, Fs=rate, noverlap=384, NFFT=512)
        ax.axis('off')
        fig.savefig('sp_xyz.png', dpi=300, frameon='false')
    
    if __name__ == '__main__': # Main function
        graph_spectrogram('...')
    

提交回复
热议问题