import json import pylab as pl from matplotlib.pylab import mpl import time import numpy as np mpl.rcParams['font.sans-serif'] = ['Microsoft YaHei'] # 显示中文 mpl.rcParams['axes.unicode_minus'] = False # 显示负号 np.set_printoptions(threshold=np.inf) # 画出时域信号和频域信号图谱 def draw_figur(data, fft_size, freqs, abs_xfft, sampling_rate, channel): fig = pl.figure() # 使用鼠标缩放图片 def call_back(event): axtemp = event.inaxes x_min, x_max = axtemp.get_xlim() fanwei = (x_max - x_min) / 10 if event.button == 'up': axtemp.set(xlim=(x_min + fanwei, x_max - fanwei)) print('up') elif event.button == 'down': axtemp.set(xlim=(x_min - fanwei, x_max + fanwei)) print('down') fig.canvas.draw_idle() # 绘图动作实时反映在图像上 fig.canvas.mpl_connect('scroll_event', call_back) fig.canvas.mpl_connect('button_press_event', call_back) pl.subplot(3, 1, 1) # 截取幕布的一部分 np.linspace(-5, 5, 10) pl.plot(data[:fft_size], color='r') pl.xlabel('点数') pl.ylabel('加速度值') pl.title('时域信号') pl.subplot(3, 1, 3) # 截取幕布的一部分 np.linspace(-5, 5, 10) pl.title(f"频域信号(通道号:{channel})") pl.ylabel("振幅") pl.xlabel("频率(KHz)") pl.plot(freqs, abs_xfft) time_name = time.time() pl.show() filename=r"E:\02.JobFile\01.项目资料\01.CMS\4.Test\mqtt接收\10B035041002230110001102\1\5_data.json" channel=0 with open(filename, 'r', encoding='utf-8') as f: data = json.load(f) data_points = list(map(float, data['data']['sample_data']['data'].split(" "))) spectrum_data = list(map(float, data['data']['sample_data']['spectrum'].split(" "))) draw_figur(data_points,60000 *10, np.linspace(0, int(60000), int(60000 *10)) /1000, spectrum_data,1, channel)