# Python 中的 Matplotlib.pyplot.specgram()来绘制频谱图

## 使用 `matplotlib.pyplot.specgram()` 方法绘制频谱图

``````matplotlib.pyplot.specgram(x,
NFFT=None,
Fs=None,
Fc=None,
detrend=None,
window=None,
noverlap=None,
cmap=None,
xextent=None,
sides=None,
scale_by_freq=None,
mode=None,
scale=None,
vmin=None,
vmax=None, *,
data=None,
**kwargs)
``````

### 示例: 使用 `matplotlib.pyplot.specgram()` 方法绘制频谱图

``````import math
import numpy as np
import matplotlib.pyplot as plt

dt=0.0001
w=2
t=np.linspace(0,5,math.ceil(5/dt))
A=20*(np.sin(3 * np.pi * t))

plt.specgram(A,Fs=1)
plt.title('Spectrogram Using matplotlib.pyplot.specgram() method')
plt.show()
``````

## 使用 `scipy.signal.spectrogram()` 方法绘制频谱图

``````import math
import numpy as np
import matplotlib.pyplot as plt
from scipy import signal

dt=0.0001
w=2
t=np.linspace(0,5,math.ceil(5/dt))
A=2*(np.sin(1 * np.pi *300* t))

f, t, Sxx = signal.spectrogram(A, fs=1, nfft=514)
plt.pcolormesh(t, f, Sxx)
plt.ylabel('Frequency')
plt.xlabel('Time')
plt.title('Spectrogram Using scipy.signal.spectrogram() method')
plt.show()
``````