script.py 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. import matplotlib.pyplot as plt
  2. import numpy as np
  3. plt.rcParams['font.sans-serif'] = ['SimHei'] # 使用黑体
  4. plt.rcParams['axes.unicode_minus'] = False # 解决负号显示问题
  5. def generate_comparison_chart():
  6. """
  7. 生成历史耗时与异步化耗时的对比折线图
  8. """
  9. # 数据定义
  10. categories = ['耗时1', '耗时2', '耗时3', '耗时4', '耗时5']
  11. # 历史耗时数据(耗时1无数据,用None表示)
  12. history_times = [730, 88, 130, 1350, 168]
  13. # 异步化耗时数据(耗时6无数据,用None表示)
  14. async_times = [696, 155, 122, 662, 135]
  15. # 创建图表
  16. plt.figure(figsize=(12, 8))
  17. # 绘制历史耗时折线
  18. plt.plot(categories, history_times,
  19. marker='o',
  20. markersize=8,
  21. linewidth=2.5,
  22. label='历史耗时',
  23. color='#3498db',
  24. alpha=0.8)
  25. # 绘制异步化耗时折线
  26. plt.plot(categories, async_times,
  27. marker='s',
  28. markersize=8,
  29. linewidth=2.5,
  30. label='异步化耗时',
  31. color='#e74c3c',
  32. alpha=0.8)
  33. # 图表美化
  34. plt.title('历史耗时 vs 异步化耗时对比分析',
  35. fontsize=16,
  36. fontweight='bold',
  37. pad=20)
  38. plt.xlabel('测试维度', fontsize=12)
  39. plt.ylabel('耗时 (ms)', fontsize=12)
  40. # 设置网格和背景
  41. plt.grid(True, linestyle='--', alpha=0.7)
  42. plt.gca().set_facecolor('#f8f9fa')
  43. # 添加图例
  44. plt.legend(fontsize=11,
  45. loc='upper right',
  46. framealpha=0.9)
  47. # 旋转x轴标签
  48. plt.xticks(rotation=45)
  49. # 自动调整布局
  50. plt.tight_layout()
  51. # 显示图表
  52. plt.show()
  53. # 打印数据统计
  54. print("数据统计:")
  55. print(
  56. 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")
  57. print(
  58. 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")
  59. if __name__ == "__main__":
  60. generate_comparison_chart()