diff --git a/myplot.png b/myplot.png index 6ed4ba4..152a6cc 100644 Binary files a/myplot.png and b/myplot.png differ diff --git a/日志处理文件.py b/日志处理文件.py index 16bdd92..dc8bd5a 100644 --- a/日志处理文件.py +++ b/日志处理文件.py @@ -1,6 +1,5 @@ import json import math - import pandas as pd import matplotlib.pyplot as plt from matplotlib import rcParams @@ -21,7 +20,6 @@ log_ones = [] start_idx = -1 for idx, one in enumerate(data): if 'Acar' == one['设备'] and '2' == one['类型']: - # print(one) if 1 == one['位置'] and 1 == one['方向']: start_idx = idx continue @@ -31,6 +29,7 @@ for idx, one in enumerate(data): log_ones.append(data[start_idx:idx+1]) start_idx = -1 print(idx) + # 筛选出只有 "Acar" 的日志片段 log_ones_new = [] for one in log_ones: @@ -46,44 +45,56 @@ for one_one in log_ones_new: df['时间差'] = df['时间戳'].diff() time_diff = df[['位置', '方向', '时间差']].dropna().reset_index(drop=True) time_diffs.append(time_diff) -# print(time_diffs) -# 找出行数最多的时间差数据集 -max_len_dataset = max(time_diffs, key=len) # 找到行数最多的 DataFrame -# print(max_len_dataset) -all_x_labels = max_len_dataset['位置'].astype(str) + '-' + max_len_dataset['方向'].astype(str) # 提取 X 轴标签 -# all_x_labels=pd.DataFrame({'位置-方向': all_x_labels}) -# 标准化所有数据集,缺少的补 0 + +# 找出行数最多的时间差数据集,以它的 (位置-方向) 组合作为标准 X 轴标签 +max_len_dataset = max(time_diffs, key=len) +all_x_labels = max_len_dataset['位置'].astype(str) + '-' + max_len_dataset['方向'].astype(str) + +# 标准化所有数据集(没有对应的“位置-方向”就补 0) standardized_time_diffs = [] -time_diffs_new = [] for time_diff in time_diffs: time_diff['位置-方向'] = time_diff['位置'].astype(str) + '-' + time_diff['方向'].astype(str) - # print(time_diff) standardized_time_diff = [] - # 手动补零 - data_idx=0 - last_is_null=0 - for idx,label in enumerate(all_x_labels): - first_row = time_diff.iloc[data_idx] - if first_row['位置-方向']==label: - if last_is_null==1: - last_is_null=0 - standardized_time_diff.append(0) + data_idx = 0 + last_is_null = 0 + for idx_label, label in enumerate(all_x_labels): + row = time_diff.iloc[data_idx] if data_idx < len(time_diff) else None + if row is not None and row['位置-方向'] == label: + # 命中当前 label + if last_is_null == 1: + last_is_null = 0 + standardized_time_diff.append(0) # 这里保持你原先的逻辑,先补 0,后面再统一填充 else: - standardized_time_diff.append(first_row['时间差']) - data_idx+=1 + standardized_time_diff.append(row['时间差']) + data_idx += 1 else: + # 没有命中当前 label standardized_time_diff.append(0) - last_is_null=1 - print(standardized_time_diff) - standardized_time_diffs.append(standardized_time_diff) - print('-------------------') + last_is_null = 1 + standardized_time_diffs.append(standardized_time_diff) + +# -------- 新增:填充 0 的函数 -------- +def fill_zeros_with_nearest(values): + """ + 将列表中的 0 用最近的非 0 值进行填充 + (先前向填充,再后向填充) + """ + # 前向填充 + for i in range(1, len(values)): + if values[i] == 0 and values[i-1] != 0: + values[i] = values[i-1] + # 后向填充 + for i in range(len(values) - 2, -1, -1): + if values[i] == 0 and values[i+1] != 0: + values[i] = values[i+1] + return values # 创建子图的行列数(自动计算) num_plots = len(all_x_labels) rows = math.ceil(math.sqrt(num_plots)) # 行数 -cols = math.ceil(num_plots / rows) # 列数 +cols = math.ceil(num_plots / rows) # 列数 # 创建大画布 fig, axes = plt.subplots(rows, cols, figsize=(16, 12)) @@ -92,22 +103,34 @@ axes = axes.flatten() # 将子图数组展平,方便迭代 # 在每个子图中绘制折线 for idx, label in enumerate(all_x_labels): # 获取当前 "位置-方向" 对应的 y 值 - y_values = [time_diff[idx] if idx < len(time_diff) else 0 for time_diff in standardized_time_diffs] + y_values = [] + for time_diff in standardized_time_diffs: + # 如果下标越界就补 0 + y_values.append(time_diff[idx] if idx < len(time_diff) else 0) + + # -------- 在绘图前,对 y_values 做一次非 0 值填充 -------- + y_values_filled = fill_zeros_with_nearest(y_values) # 绘制当前子图 ax = axes[idx] ax.plot( - range(len(standardized_time_diffs)), # x轴为不同的时间差数据集序号 - y_values, # y轴为对应的时间差 - marker='o', # 标记点样式 + range(len(y_values_filled)), + y_values_filled, + marker='o', ) + ax.set_ylim(0, 10000) + # 设置标题和轴标签 ax.set_title(f'{label} 的时间差折线图', fontsize=10) ax.set_xlabel('标准化时间差数据集', fontsize=8) ax.set_ylabel('时间差 (ms)', fontsize=8) - ax.set_xticks(range(len(standardized_time_diffs))) - ax.set_xticklabels([f'TimeDiff {i+1}' for i in range(len(standardized_time_diffs))], fontsize=6, rotation=45) + ax.set_xticks(range(len(y_values_filled))) + ax.set_xticklabels( + [f'TimeDiff {i+1}' for i in range(len(y_values_filled))], + fontsize=6, + rotation=45 + ) ax.grid(axis='y', linestyle='--', alpha=0.7) # 删除多余的子图(如果子图数量多于折线图数量) @@ -116,4 +139,4 @@ for ax in axes[num_plots:]: # 调整布局 plt.tight_layout() -plt.show() \ No newline at end of file +plt.show() diff --git a/日志处理文件_真.py b/日志处理文件_真.py new file mode 100644 index 0000000..9024068 --- /dev/null +++ b/日志处理文件_真.py @@ -0,0 +1,120 @@ +import json +import math + +import pandas as pd +import matplotlib.pyplot as plt +from matplotlib import rcParams + +# 设置字体为 SimHei(黑体)或其他支持中文的字体 +rcParams['font.sans-serif'] = ['SimHei'] # 或者 ['Microsoft YaHei'] +rcParams['axes.unicode_minus'] = False # 解决负号显示问题 + +# 指定 JSON 文件的路径 +file_path = r"C:\Users\10561\Desktop\2025-01-06_应用日志.json" + +# 打开并读取 JSON 文件 +with open(file_path, 'r', encoding='utf-8') as file: + data = json.load(file) + +# 提取指定设备和类型的日志片段 +log_ones = [] +start_idx = -1 +for idx, one in enumerate(data): + if 'Acar' == one['设备'] and '2' == one['类型']: + # print(one) + if 1 == one['位置'] and 1 == one['方向']: + start_idx = idx + continue + if 8 == one['位置'] and 2 == one['方向']: + if start_idx == -1: + continue + log_ones.append(data[start_idx:idx+1]) + start_idx = -1 + print(idx) +# 筛选出只有 "Acar" 的日志片段 +log_ones_new = [] +for one in log_ones: + one_only_car = [one_one for one_one in one if 'Acar' == one_one['设备']] + log_ones_new.append(one_only_car) +[print(o) for o in log_ones_new] + +# 计算时间差 +time_diffs = [] +for one_one in log_ones_new: + df = pd.DataFrame(one_one) + df['时间戳'] = pd.to_numeric(df['时间戳']) + df['时间差'] = df['时间戳'].diff() + time_diff = df[['位置', '方向', '时间差']].dropna().reset_index(drop=True) + time_diffs.append(time_diff) +# print(time_diffs) +# 找出行数最多的时间差数据集 +max_len_dataset = max(time_diffs, key=len) # 找到行数最多的 DataFrame +# print(max_len_dataset) +all_x_labels = max_len_dataset['位置'].astype(str) + '-' + max_len_dataset['方向'].astype(str) # 提取 X 轴标签 +# all_x_labels=pd.DataFrame({'位置-方向': all_x_labels}) +# 标准化所有数据集,缺少的补 0 +standardized_time_diffs = [] +time_diffs_new = [] +for time_diff in time_diffs: + time_diff['位置-方向'] = time_diff['位置'].astype(str) + '-' + time_diff['方向'].astype(str) + # print(time_diff) + standardized_time_diff = [] + + # 手动补零 + data_idx=0 + last_is_null=0 + for idx,label in enumerate(all_x_labels): + first_row = time_diff.iloc[data_idx] + if first_row['位置-方向']==label: + if last_is_null==1: + last_is_null=0 + standardized_time_diff.append(0) + else: + standardized_time_diff.append(first_row['时间差']) + data_idx+=1 + else: + standardized_time_diff.append(0) + last_is_null=1 + print(standardized_time_diff) + standardized_time_diffs.append(standardized_time_diff) + print('-------------------') + + +# 创建子图的行列数(自动计算) +num_plots = len(all_x_labels) +rows = math.ceil(math.sqrt(num_plots)) # 行数 +cols = math.ceil(num_plots / rows) # 列数 + +# 创建大画布 +fig, axes = plt.subplots(rows, cols, figsize=(16, 12)) +axes = axes.flatten() # 将子图数组展平,方便迭代 + +# 在每个子图中绘制折线 +for idx, label in enumerate(all_x_labels): + # 获取当前 "位置-方向" 对应的 y 值 + y_values = [time_diff[idx] if idx < len(time_diff) else 0 for time_diff in standardized_time_diffs] + + # 绘制当前子图 + ax = axes[idx] + ax.plot( + range(len(standardized_time_diffs)), # x轴为不同的时间差数据集序号 + y_values, # y轴为对应的时间差 + marker='o', # 标记点样式 + ) + ax.set_ylim(0, 10000) + +# 设置标题和轴标签 + ax.set_title(f'{label} 的时间差折线图', fontsize=10) + ax.set_xlabel('标准化时间差数据集', fontsize=8) + ax.set_ylabel('时间差 (ms)', fontsize=8) + ax.set_xticks(range(len(standardized_time_diffs))) + ax.set_xticklabels([f'TimeDiff {i+1}' for i in range(len(standardized_time_diffs))], fontsize=6, rotation=45) + ax.grid(axis='y', linestyle='--', alpha=0.7) + +# 删除多余的子图(如果子图数量多于折线图数量) +for ax in axes[num_plots:]: + fig.delaxes(ax) + +# 调整布局 +plt.tight_layout() +plt.show() \ No newline at end of file