如何在Python的Matplotlib中绘制幅度谱? 要绘制幅度谱,可以执行以下步骤− 设置图形大小和子图之间以及周围的填充。 获取随机种子。 初始化采样间隔 dt ,并找到采样频率。 为 t 创建随机数据点。 要生成噪声, 使用numpy获取nse、r、cnse 和 s 使用subplots()方法创建一个图形和一组子图。 设置图的标题。 绘制幅度谱。 使用 show() 方法显示图形。 示例 import matplotlib.pyplot as plt import numpy as np plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True np.random.seed(0) dt = 0.01 # 采样间隔 Fs = 1 / dt # 采样频率 t = np.arange(0, 10, dt) # 生成噪声: nse = np.random.randn(len(t)) r = np.exp(-t / 0.05) cnse = np.convolve(nse, r) * dt cnse = cnse[:len(t)] s = 0.1 * np.sin(4 * np.pi * t) + cnse fig, axs = plt.subplots() axs.set_title("幅度谱") axs.magnitude_spectrum(s, Fs=Fs, color='C1') plt.show() PythonCopy 输出