这是将一个表格中的成绩通过xlrd 打开将成绩一览输出,然后将里面的成绩进行比较,得出不同的等级,这些等级再形成一个列表,根据这些做出一个图
import xlrd
from matplotlib import pyplot as plt
#表格属性为姓名+语文+数学+英语+科学 对应这几个字段
class Score_level_map:
scoreList = []
levelList = []
def __init__(self):
self.file = xlrd.open_workbook("C:/Users/suoer/Desktop/1.xls")
def getSource(self):
table=self.file.sheets()[0]
for i in range(table.nrows):
if i!=0:
data =table.row_values(i)
self.scoreList.append(data)
def getLevel(self):
for i in self.scoreList:
list = [i[0]]
for s in i:
if type(s)!=str:
if s>=90:
list.append("优秀")
elif 80<=s<=89:
list.append("良好")
elif 70<=s<=79:
list.append("中等")
elif 60<=s<=69:
list.append("及格")
elif s<60:
list.append("不及格")
self.levelList.append(list)
print(self.levelList)
#只是一个学科的统计图
def getShape(self):
fig = plt.figure()
labels = '优秀', '良好', '中等', '及格','不及格'
sizes = []
a=0
b=0
c=0
d=0
e=0
for i in self.levelList:
if i[1]=="优秀":
a+=1
elif i[1]=="良好":
b+=1
elif i[1]=="中等":
c+=1
elif i[1]=="及格":
d+=1
elif i[1]=="不及格":
e+=1
Sum=a+b+c+d+e
sizes.append(a/Sum)
sizes.append(b/Sum)
sizes.append(c/Sum)
sizes.append(d/Sum)
sizes.append(e/Sum)
explode = (0, 0, 0, 0, 0) # only "explode" the 2nd slice (i.e. 'Hogs')
fig1, (ax1, ax2) = plt.subplots(2)
ax1.pie(sizes, labels=labels, autopct='%1.1f%%', shadow=True)
ax1.axis('equal')
ax2.pie(sizes, autopct='%1.1f%%', shadow=True, startangle=90, explode=explode,
pctdistance=1.12)
ax2.axis('equal')
ax2.legend(labels=labels, loc='upper right')
plt.show()
def main(self):
self.getSource()
self.getLevel()
self.getShape()
if __name__=="__main__":
Score_level_map().main()