题目要求
综合项目设计2:成绩分析及可视化实例
【已有功能】某班有30名学生的3门课程成绩,请统计每个学生课程的总分、平均分,每门课程的最高分、最低分,并绘制图形,对比各成绩段的成绩人数分布。
【教学目标】强化numpy和matplotlib的应用能力,numpy读取csv。
【已有代码】
#成绩分析及可视化实例
import numpy as np
import matplotlib
import matplotlib.pyplot as plt
matplotlib.rcParams['font.family'] = 'SimHei'
stuScore = np.loadtxt('student_score.csv',delimiter = ',')#读入成绩文件,返回数组
sumEach = np.sum(stuScore[:,1:],axis = 1)#返回每个学生3门课程总分
avgEach = np.average(stuScore[:,1:],axis = 0)#返回每个学生每门课程平均分
#返回最高分和最低分
maxMath = np.max(stuScore[:,1])
maxEng = np.max(stuScore[:,2])
maxPython = np.max(stuScore[:,3])
minMath = np.min(stuScore[:,1])
minEng = np.min(stuScore[:,2])
minPython = np.min(stuScore[:,3])
print("个人总分情况是:")
print(sumEach)
print("个人平均分情况是:")
print(avgEach)
print("班级每门课程最高分:")
print(maxMath,maxEng,maxPython)
print("班级每门课程最低分:")
print(minMath,minEng,minPython)
#取出各科成绩
mathScore = stuScore[:,1]
engScore = stuScore[:,2]
pythonScore = stuScore[:,3]
#绘制高数直方图
plt.suptitle("成绩分布直方图")
plt.subplot(3,1,1)
plt.hist(mathScore,bins=10,range=(0,100),color='red')# 0-100分,分成10段
plt.xlabel("高数成绩分数段")#设置x轴标签
plt.ylabel("人数")#设置y轴标签
plt.xlim(0,100)#设置x轴区间
plt.ylim(0,20)#设置y轴区间
plt.xticks([0,10,20,30,40,50,60,70,80,90,100])#设置x轴刻度
plt.yticks([0,5,10,15,20]) #设置y轴刻度
plt.grid()
#绘制英语直方图
plt.subplot(3,1,2)
plt.hist(engScore,bins=10,range=(0,100),color='blue')#0-100分,分成10段
plt.xlabel("英语成绩分数段")#设置x轴标签
plt.ylabel("人数")#设置y轴标签
plt.xlim(0,100)#设置x轴区间
plt.ylim(0,20)#设置y轴区间
plt.xticks([0,10,20,30,40,50,60,70,80,90,100])#设置x轴刻度
plt.yticks([0,5,10,15,20]) #设置y轴刻度
plt.grid()
#绘制python直方图
plt.suptitle("成绩分布直方图")
plt.subplot(3,1,3)
plt.hist(pythonScore,bins=10,range=(0,100),color='green')#0-100分,分成10段
plt.xlabel("Python成绩分数段")#设置x轴标签
plt.ylabel("人数")#设置y轴标签
plt.xlim(50,100)#设置x轴区间
plt.ylim(0,20)#设置y轴区间
plt.xticks([50,60,70,80,90,100])#设置x轴刻度
plt.yticks([0,5,10,15,20]) #设置y轴刻度
plt.grid()
plt.show()
项目修改与设计说明:
(1)绘制三门课程平均分的柱状图对比;(2)绘制python课程各成绩段人数的饼图;
实现代码
import numpy as np
import matplotlib
import matplotlib.pyplot as plt
matplotlib.rcParams['font.family'] = 'SimHei'
stuScore = np.loadtxt('student_score.csv',delimiter = ',')
sumEach = np.sum(stuScore[:,1:],axis = 1)
avgEach = np.average(stuScore[:,1:],axis = 0)
maxMath = np.max(stuScore[:,1])
maxEng = np.max(stuScore[:,2])
maxPython = np.max(stuScore[:,3])
minMath = np.min(stuScore[:,1])
minEng = np.min(stuScore[:,2])
minPython = np.min(stuScore[:,3])
print(avgEach)
print("个人总分情况是:")
print(sumEach)
print("个人平均分情况是:")
print(avgEach)
print("班级每门课程最高分:")
print(maxMath,maxEng,maxPython)
print("班级每门课程最低分:")
print(minMath,minEng,minPython)
mathScore = stuScore[:,1]
engScore = stuScore[:,2]
pythonScore = stuScore[:,3]
bar = plt.subplot(1,2,1)
x=['高数','英语','python']
plt.bar(x,avgEach)
plt.yticks([10,20,30,40,50,60,70,80,90,100])
a=0
b=0
c=0
d=0
e=0
list1=[]
for row in stuScore:
list1.append(row[3])
if(row[3]<60):
e=e+1
if(60<=row[3]and row[3]<70):
d=d+1
if(70<=row[3]and row[3]<80):
c=c+1
if(80<=row[3]and row[3]<90):
b=b+1
if(90<=row[3]):
a=a+1
edu = [a/30,b/30,c/30,d/30,e/30]
labels = ['优秀','良', '中','及格','不及格']
explode = [0.1,0,0,0,0]
colors=['#9999ff','#ff9999','#7777aa','#2442aa','#dd5555']
plt.rcParams['font.sans-serif'] = ['Microsoft YaHei']
plt.rcParams['axes.unicode_minus'] = False
plt.axes(aspect='equal')
plt.xlim(0,4)
plt.ylim(0,4)
pei = plt.subplot(1,2,2)
plt.pie(x = edu,
explode=explode,
labels=labels,
colors=colors,
autopct='%.1f%%',
pctdistance=0.8,
labeldistance = 1.15,
startangle = 180,
radius = 1.5,
counterclock = False,
wedgeprops = {'linewidth': 1.5, 'edgecolor':'green'},
textprops = {'fontsize':12, 'color':'k'},
center = (1.8,1.8),
frame = 0 )
plt.xticks(())
plt.yticks(())
plt.show()
代码运行截图:
|