1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
| from itertools import groupby import matplotlib.pyplot as plt
plt.rcParams['font.sans-serif']=['simhei']
scores={'数据结构':[89,70,49,87,92,84,73,71,78,81,90,37, 77,82.81,79,80,82,75,90,54,80,70,68,61], '线性代数':[70,74,80,60,50,87,68,77,95,80,79,74, 69,64,82,81,78,90,78,79,72,69,45,70,70,79], '英语':[83,87,69,55,80,89,96,81,83,90,5470,79, 66,85,82,88,76,60,80,75,83,75,70,20], 'Python':[90,60,82,79,88,92,85,87,89,71,45,50, 80,81,87,93,80,70,68,65,85,89,80,72,75]}
def splitscore(score): if score>=85: return '优' elif score>=60: return '及格' else: return '不及格'
ratios=dict() for subject,subjectScore in scores.items(): ratios[subject]={} for category,num in groupby(sorted(subjectScore),splitscore): ratios[subject][category]=len(tuple(num))
fig,axs=plt.subplots(2,2) axs.shape=4,
for index, subjectData in enumerate(ratios.items()): plt.sca(axs[index]) subjectName, subjectRatio = subjectData plt.pie(list(subjectRatio.values()), labels=list(subjectRatio.keys()), autopct='%1.1f%%') plt.xlabel(subjectName) plt.legend() plt.gca().set_aspect('equal') plt.show()
|