如何使用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("Longitudinal Magnitude Spectrum") axs.magnitude_spectrum(s, Fs=Fs, scale='dB', color='C1') plt.show() PythonCopy 输出