1.各科平均分表格如下图

各班各科平均分
2.代码实现:
import pandas as pdimport matplotlib.pyplot as pltdef plot_subject_scores(): try: # 读取平均分数据 df = pd.read_excel('平均分.xlsx') # 设置中文字体 plt.rcParams['font.sans-serif'] = ['SimHei'] plt.rcParams['axes.unicode_minus'] = False # 定义学科列表 subjects = ['语文', '数学', '英语', '科学', '历史与社会'] # 为每个学科创建单独的条形图 for subject in subjects: plt.figure(figsize=(12, 6)) # 创建条形图 bars = plt.bar(df['班级'], df[subject], color='#1f77b4') # 在条形上方添加数值标签 for bar in bars: height = bar.get_height() plt.text(bar.get_x() + bar.get_width()/2., height, f'{height:.1f}', ha='center', va='bottom') # 设置图表属性 plt.title(f'{subject}平均分分布') plt.xlabel('班级') plt.ylabel('平均分') plt.xticks(rotation=45) plt.grid(True, axis='y', linestyle='--', alpha=0.7) # 调整布局 plt.tight_layout() # 保存图表 plt.savefig(f'{subject}平均分统计图.png', dpi=300, bbox_inches='tight') print(f"已保存:{subject}平均分统计图.png") print("所有统计图生成完成!") except FileNotFoundError: print("错误:找不到平均分.xlsx文件") except Exception as e: print(f"发生错误:{str(e)}")if __name__ == '__main__': plot_subject_scores()
例:各班语文平均分条形统计图

各班数学平均分条形统计图