fork download
  1. import matplotlib.pyplot as plt
  2. import numpy as np
  3. import pandas as pd
  4.  
  5. # 1. 准备数据
  6. # 和PPT一样的结构:行是考点,列是年份
  7. data = {
  8. '考点': ['新陈代谢', '遗传与变异', '稳态与调节', '基因工程与生物技术安全', '其余综合考点'],
  9. '2023年': [25, 21, 16, 9, 29],
  10. '2024年': [26, 22, 15, 10, 27],
  11. '2025年': [26, 23, 16, 11, 24]
  12. }
  13. df = pd.DataFrame(data)
  14.  
  15. # 2. 设置图表风格和配色
  16. plt.figure(figsize=(12, 6)) # 宽屏比例,适合横向展示
  17. colors = ['#4e79a7', '#f28e2b', '#e15759'] # 蓝、橙、红,对比明显
  18.  
  19. # 3. 设置水平分组条形图
  20. y = np.arange(len(df['考点'])) # 确定y轴位置
  21. height = 0.25 # 柱子的高度(厚度)
  22.  
  23. # 4. 画出三个年份的条形(用偏移量把它们并排放在一起)
  24. # 2023年:往左偏移一个位置
  25. plt.barh(y - height, df['2023年'], height=height, label='2023年', color=colors[0])
  26. # 2024年:居中
  27. plt.barh(y, df['2024年'], height=height, label='2024年', color=colors[1])
  28. # 2025年:往右偏移一个位置
  29. plt.barh(y + height, df['2025年'], height=height, label='2025年', color=colors[2])
  30.  
  31. # 5. 添加数值标签
  32. for i, v in enumerate(df['2023年']):
  33. plt.text(v + 0.5, i - height, f'{v}%', ha='left', va='center', fontsize=9)
  34. for i, v in enumerate(df['2024年']):
  35. plt.text(v + 0.5, i, f'{v}%', ha='left', va='center', fontsize=9)
  36. for i, v in enumerate(df['2025年']):
  37. plt.text(v + 0.5, i + height, f'{v}%', ha='left', va='center', fontsize=9)
  38.  
  39. # 6. 图表装饰(标题、标签、图例等)
  40. plt.xlabel('占比 (%)', fontsize=12)
  41. plt.title('2023-2025年 高考生物核心考点分值占比趋势', fontsize=14, pad=15)
  42. plt.yticks(y, df['考点'], fontsize=11) # 替换为中文考点名称
  43. plt.legend(title='年份', loc='lower right', fontsize=10)
  44. plt.grid(axis='x', linestyle='--', alpha=0.3) # 显示竖线网格,方便阅读
  45. plt.xlim(0, 35) # 预留一部分空间给标签
  46.  
  47. # 7. 显示/保存
  48. plt.tight_layout()
  49. plt.show()
Success #stdin #stdout 1.15s 85996KB
stdin
Standard input is empty
stdout
Standard output is empty