用pyecharts画饼图对分数进行分类并统计各个分数段的占比
时间: 2024-01-10 16:03:59 浏览: 82
好的,以下是实现该功能的代码:
```python
from pyecharts import options as opts
from pyecharts.charts import Pie
# 模拟分数数据
scores = [78, 85, 92, 68, 80, 88, 75, 90, 72, 82, 95, 60]
# 分数分类
bins = [0, 60, 70, 80, 90, 100]
score_categories = ["不及格", "及格", "中等", "良好", "优秀"]
score_counts = [0] * len(bins)
for score in scores:
for i in range(len(bins)):
if score >= bins[i] and score < bins[i+1]:
score_counts[i] += 1
break
# 统计各个分数段的占比
total_count = len(scores)
score_percentages = [count/total_count*100 for count in score_counts]
# 画饼图
pie = (
Pie()
.add("", [list(z) for z in zip(score_categories, score_percentages)])
.set_series_opts(label_opts=opts.LabelOpts(formatter="{b}: {c}%"))
.set_global_opts(title_opts=opts.TitleOpts(title="分数分类饼图"))
)
pie.render("score_pie.html")
```
这段代码首先模拟了一些分数数据,然后将数据按照一定的规则分类,统计各个分数段的数量和占比,最后用 Pyecharts 画出了饼图,并保存在 `score_pie.html` 文件中。
阅读全文