| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182 |
- import time
- from tarfile import data_filter
- import pandas as pd
- import matplotlib.pyplot as plt
- from datetime import datetime
- from pandas.io.sas.sas_constants import dataset_length
- # 示例数据(一组数据即可)
- data = [
- {"time": 1114, "params": {"key1": 190, "key2": 328, "key3": 312}},
- {"time": 1113, "params": {"key1": 150, "key2": 318, "key3": 334}},
- {"time": 1112, "params": {"key1": 126, "key2": 299, "key3": 340}},
- {"time": 1111, "params": {"key1": 119, "key2": 270, "key3": 350}},
- {"time": 1110, "params": {"key1": 110, "key2": 200, "key3": 100}}
- ]
- class DataVisualizer:
- def __init__(self, data_list):
- self.data_list = data_list
- self.dfs = self._process_data_list()
- self.keys = self._get_keys() # 自动获取所有key
- # 添加中文字体配置
- self._setup_chinese_font()
- def _setup_chinese_font(self):
- """配置中文字体支持"""
- try:
- plt.rcParams['font.sans-serif'] = ['SimHei', 'Microsoft YaHei', 'DejaVu Sans']
- plt.rcParams['axes.unicode_minus'] = False
- except Exception as e:
- print(f"字体配置警告: {e}")
- def _process_data_list(self):
- """处理多个原始数据为DataFrame列表"""
- dfs = []
- print(self.data_list)
- for data in self.data_list:
- df_data = []
- for item in data:
- row = {"time": item["time"]}
- row.update(item["params"])
- df_data.append(row)
- df = pd.DataFrame(df_data)
- df = df.sort_values('time') # 按时间升序排列
- dfs.append(df)
- return dfs
- def _get_keys(self):
- """自动获取所有key(即params中的键)"""
- if self.dfs:
- return list(self.dfs[0].columns) # 假设所有数据的key一致
- return []
- def get_data_summary(self):
- """生成多个数据数组的统计摘要"""
- summaries = []
- for df in self.dfs:
- summary = {'数据点数量': len(df), '时间范围': f"{df['time'].min()} - {df['time'].max()}"}
- for key in self.keys:
- start = df[key].iloc[0]
- end = df[key].iloc[-1]
- diff = end - start
- rate = (diff / start) * 100 if start != 0 else 0
- summary[f'{key}变化'] = f"{start} → {end} (Δ{diff}, {rate:.1f}%)"
- summaries.append(summary)
- return summaries
- def convertTime(timesMs):
- local_time = time.localtime(timesMs)
- return f"{local_time.tm_hour:02d}:{local_time.tm_min:02d}"
- def create_individual_key_plots11(self):
- """为每个key单独生成一个独立图表"""
- for key in self.keys:
- if key == 'time':
- continue
- plt.figure(figsize=(10, 6))
- for i, df in enumerate(self.dfs):
- plt.plot(df['time'], df[key],
- marker='o', linewidth=2, markersize=8,
- label=f'数据组{i + 1} - {key}变化趋势')
- print('xx = ' , df['time'])
- print('mm = ', df[key])
- for j, (time, value) in enumerate(zip(df['time'], df[key])):
- plt.annotate(f'{value}', (time, value), textcoords="offset points", xytext=(0, 10), ha='center', fontsize=12)
- plt.xlabel('时间', fontsize=12)
- plt.ylabel('参数值', fontsize=12)
- plt.title(f'{key}在不同数据组随时间的变化对比', fontsize=14, fontweight='bold')
- plt.legend()
- plt.grid(True, alpha=0.3)
- plt.xticks(self.dfs[0]['time'])
- plt.tight_layout()
- plt.show()
- def create_individual_key_plots(self):
- """为每个key单独生成一个独立图表,时间戳转换为年月日时分秒格式"""
- for key in self.keys:
- if key == 'time':
- continue
- plt.figure(figsize=(10, 6))
- # 存储所有格式化后的时间,用于统一x轴刻度
- all_formatted_times = []
- for i, df in enumerate(self.dfs):
- # 将时间戳转换为年月日时分秒格式
- formatted_times = []
- for timestamp in df['time']:
- # 假设时间戳为整数或浮点数
- dt_obj = datetime.fromtimestamp(timestamp / 1000)
- formatted_time = dt_obj.strftime("%H:%M:%S")
- formatted_times.append(formatted_time)
- # 如果是第一个数据组,保存格式化后的时间用于x轴刻度
- if i == 0:
- all_formatted_times = formatted_times
- # 使用格式化后的时间进行绘图
- plt.plot(formatted_times, df[key],
- marker='o', linewidth=2, markersize=8,
- label=f'数据组{i + 1} - {key}变化趋势')
- print('原始时间戳 = ', df['time'])
- print('格式化时间 = ', formatted_times)
- print('数值数据 = ', df[key])
- # 为每个数据点添加标注
- for j, (formatted_time, value) in enumerate(zip(formatted_times, df[key])):
- plt.annotate(f'{value}',
- (formatted_time, value),
- textcoords="offset points",
- xytext=(0, 10),
- ha='center',
- fontsize=12)
- plt.xlabel('时间', fontsize=12)
- plt.ylabel('参数值', fontsize=12)
- plt.title(f'{key}在不同数据组随时间的变化对比', fontsize=14, fontweight='bold')
- plt.legend()
- plt.grid(True, alpha=0.3)
- # 设置x轴刻度为格式化后的时间
- plt.xticks(all_formatted_times, rotation=45)
- plt.tight_layout()
- plt.show()
- def show(data1):
- # 创建数据数组(示例:1个数据数组)
- data_list = [data1]
- # 创建可视化器实例
- visualizer = DataVisualizer(data_list)
- print("=" * 50)
- print("数据表格展示")
- print("=" * 50)
- for i, df in enumerate(visualizer.dfs):
- print(f"数据组{i + 1}表格:")
- print(df)
- print()
- print("=" * 50)
- print("数据统计摘要")
- print("=" * 50)
- summaries = visualizer.get_data_summary()
- for i, summary in enumerate(summaries):
- print(f"数据组{i + 1}统计摘要:")
- for key, value in summary.items():
- print(f" {key}: {value}")
- print()
- # 为每个key单独生成图表
- print("正在生成每个key的独立对比图表...")
- visualizer.create_individual_key_plots()
- '''
- if __name__ == "__main__":
- main()
- '''
|