问题描述
我想从 wav 文件中获取频谱图,然后将其保存为 png,但我只需要图像的内容(而不是轴或其他任何内容).我遇到了这些问题
也许它不可见,但是内容周围有一个白色边框(从最大到最小):左,下,上,右.我想要相同的图像,但只是内容,没有其他任何东西.我怎样才能做到这一点?我使用python 3.6和Matplotlib 2.0.2.
我认为您想要subplots_adjust
:
fig,ax = plt.subplots(1)fig.subplots_adjust(left = 0,right = 1,bottom = 0,top = 1)ax.axis('tight')ax.axis('off')
在这种情况下:
将matplotlib.pyplot导入为plt从 scipy.io 导入 wavfile将numpy导入为npdef graph_spectrogram(wav_file):速率,数据 = wavfile.read(wav_file)图,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')如果__name__ =='__main__':#主函数graph_spectrogram('...')
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 questions
Matplotlib plots: removing axis, legends and white spaces
scipy: savefig without frames, axes, only content
I've also read the Matplotlib documentation but it seems useless and so either answers to questions above are outdated or I'm doing something wrong because simple
plt.savefig('out.png', bbox_inches='tight', pad_inches=0)
does not do what I want to achieve. Initially I tried to follow this guide but the code crashes. Then I tried this approach, but since it's outdated I modified it a little:
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)
pxx, freqs, bins, im = plt.specgram(x=data, Fs=rate, noverlap=384, NFFT=512)
plt.axis('off')
plt.savefig('sp_xyz.png', bbox_inches='tight', dpi=300, frameon='false')
if __name__ == '__main__': # Main function
graph_spectrogram('...')
This is what I got:
Maybe it's not visible, but there's a white border around the content (from the biggest to the smallest): left, bottom, top, right. I want the same image but just the content without anything else. How can I achieve that? I use python 3.6 and Matplotlib 2.0.2.
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('...')
这篇关于使用 Matloptlib 将图像(只有内容,没有轴或其他任何东西)保存到文件中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!