Python应用:利用matplotlib画学生成绩分布饼图

在这里插入图片描述

1. 题目

  给定一组学生成绩:[85, 92, 78, 65, 95, 88, 72, 60, 98, 45, 100, 46, 23, 88, 67, 89, 67, 88, 99],现在评分等级为优(90-100)、良(70-89)、及格(60-69)、不及格(0-59),请根据上述信息画出学生成绩等级分布图。

2. 安装

在这里插入图片描述
  通过命令安装:

pip install matplotlib

3. 代码实现

import matplotlib.pyplot as plt

# 设置全局字体为支持中文的字体,例如SimHei(黑体)
plt.rcParams['font.sans-serif'] = 'SimHei'
plt.rcParams['axes.unicode_minus'] = False  # 解决负号显示问题

# 假设这是学生语文成绩数据
chinese_scores = [85, 92, 78, 65, 95, 88, 72, 60, 98, 45,100,46,23,88,67,89,67,88,99]

# 分数段定义
excellent = [score for score in chinese_scores if 90 <= score <= 100]
good = [score for score in chinese_scores if 70 <= score <= 89]
passing = [score for score in chinese_scores if 60 <= score <= 69]
fail = [score for score in chinese_scores if score < 60]

# 分组名称
labels = ['优秀', '良好', '及格', '不及格']

# 分组数据
data = [len(excellent), len(good), len(passing), len(fail)]

# 绘制饼图
plt.pie(data, labels=labels, autopct='%1.1f%%', startangle=140, colors=['green', 'yellow', 'orange', 'red'])
plt.axis('equal')  # 保持图形为正圆形
plt.title('学生语文成绩分布图')

# 显示中文标签在饼图的中心
plt.text(0, 0, '学生\n语文成绩', ha='center', va='center', fontproperties='SimHei', fontsize=12, weight='bold', color='black')

plt.show()

4. 运行效果

在这里插入图片描述

5. 参考

https://matplotlib.org/stable/users/index

在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

snail哥

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值