import matplotlib.pyplot as plt import numpy as np plt.rcParams['font.sans-serif'] = ['SimHei'] # 使用黑体 plt.rcParams['axes.unicode_minus'] = False # 解决负号显示问题 def generate_comparison_chart(): """ 生成历史耗时与异步化耗时的对比折线图 """ # 数据定义 categories = ['耗时1', '耗时2', '耗时3', '耗时4', '耗时5'] # 历史耗时数据(耗时1无数据,用None表示) history_times = [730, 88, 130, 1350, 168] # 异步化耗时数据(耗时6无数据,用None表示) async_times = [696, 155, 122, 662, 135] # 创建图表 plt.figure(figsize=(12, 8)) # 绘制历史耗时折线 plt.plot(categories, history_times, marker='o', markersize=8, linewidth=2.5, label='历史耗时', color='#3498db', alpha=0.8) # 绘制异步化耗时折线 plt.plot(categories, async_times, marker='s', markersize=8, linewidth=2.5, label='异步化耗时', color='#e74c3c', alpha=0.8) # 图表美化 plt.title('历史耗时 vs 异步化耗时对比分析', fontsize=16, fontweight='bold', pad=20) plt.xlabel('测试维度', fontsize=12) plt.ylabel('耗时 (ms)', fontsize=12) # 设置网格和背景 plt.grid(True, linestyle='--', alpha=0.7) plt.gca().set_facecolor('#f8f9fa') # 添加图例 plt.legend(fontsize=11, loc='upper right', framealpha=0.9) # 旋转x轴标签 plt.xticks(rotation=45) # 自动调整布局 plt.tight_layout() # 显示图表 plt.show() # 打印数据统计 print("数据统计:") print( f"历史耗时范围: {min([x for x in history_times if x is not None])} - {max([x for x in history_times if x is not None])} ms") print( f"异步化耗时范围: {min([x for x in async_times if x is not None])} - {max([x for x in async_times if x is not None])} ms") if __name__ == "__main__": generate_comparison_chart()