生活随笔
收集整理的這篇文章主要介紹了
python 学生成绩表,生成数据表并且绘图
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
在學習python的過程中,老師出了一道題,感覺還不錯,就寫個博客記錄一下:
1.已知有50個學生,期末參加5門考試,每一門的權重是(0.3,0.2,0.2,0.2,0.1)
1)隨機生成學生基本信息(姓名,學號)
2)隨機生成每個學生的成績
3)計算每個學生的總評成績
4)統計總評成績中各個分數段的人數,并畫出柱狀圖
5)計算每門課的均值與方差,并用折線圖顯示出來
import numpy as np
import matplotlib
.pyplot as plt
import pandas as pd
import random as rm
import random
import matplotlib#成績生成器
def
get(n
):temp
= []for j in
range(n
):x
= rm
.randint(60, 100) # 返回
1->100中的任意整數temp
.append(x
) # 在數組后面加上相應的元素x# 建立了一個二維列表 c
[[],[],[]....]return temp#名字生成器
def
random_name(j
):list_Xing
= ['趙', '錢', '孫', '李', '周', '吳', '鄭', '王', '馮', '陳', '褚', '衛', '蔣', '沈', '韓', '楊', '張', '李']list_Ming
= ['豫', '章', '故', '郡', '洪', '都', '新', '府', '星', '分', '翼', '軫', '地', '接', '衡', '廬', '襟', '三', '江', '', '而','帶', '五', '湖', '控', '蠻', '荊', '而', '引', '甌', '越', '物', '華', '天', '寶', '龍', '光', '射', '牛', '斗', '之','墟', '人', '杰', '地', '靈', '徐', '孺', '餞', '子']list_studentname
= []for i in
range(j
):name
= random
.choice(list_Xing
) + random
.choice(list_Ming
) + random
.choice(list_Ming
)list_studentname
.append(name
)# 將隨機生成的五個姓名生成一個列表list_student
return list_studentname#柱狀圖生成器
def
map_1(y
):matplotlib
.rcParams
['font.sans-serif'] = ['YouYuan']matplotlib
.rcParams
['axes.unicode_minus'] = Falsefig
= plt
.figure(facecolor
='#87CEEB')label_list
= ['60~70', '70~80', '80~90', '90~100'] # 橫坐標刻度顯示值num_list1
= y # 縱坐標值
1x
= range(len(num_list1
))# 繪制條形圖rects1
= plt
.bar(x
, height
=num_list1
, width
=0.4, alpha
=0.5, color
='red', label
='學生')# 設置y軸屬性plt
.ylim(0, 50)plt
.ylabel('人數')# 設置x軸屬性plt
.xticks([index
+ 0.2 for index in x
], label_list
)plt
.xlabel("成績")plt
.title('學生成績分布圖')plt
.legend()# 顯示文本
for rect in rects1
:height
= rect
.get_height()plt
.text(rect
.get_x() + rect
.get_width() / 2, height
+ 1, str(height
), ha
='center', va
='bottom')plt
.show()#折線圖生成器
def
map_2(X
, Y
):x
= ['yuwen', 'shuxue', 'yinyu', 'pthon', 'cpp']y
= Y_y
= Xplt
.plot(x
, y
, 'rs-', x
, _y
, "bo-")plt
.show()if __name__
== "__main__":#生成學號no
= []for i in
range(1, 51):no
.append(i
)#使用字典data
= {"name":random_name(50) , "yuwen": get(50), "shuxue": get(50), "yinyu": get(50), "pthon": get(50), "cpp": get(50)}#使用DataFrame生成一個表df
= pd
.DataFrame(data
, no
)#定義 列表hole 保存所有學生的總評成績hole
= []for i in
range(1, 51):a
= df
.yuwen
[i
] * 0.3 + df
.shuxue
[i
] * 0.2 + df
.yinyu
[i
] * 0.2 + df
.pthon
[i
] * 0.2 + df
.cpp
[i
] * 0.1hole
.append(a
)#建一個表 存儲總評成績data3
= {}df3
= pd
.DataFrame(data3
, no
)data2
= {"hole": hole
}df2
= pd
.DataFrame(data2
, no
)#將兩個表合并,并打印df3
= pd
.concat([df
, df2
], axis
=1)print(df3
)#統計各個分數段(暫定四段)的人數Y2
= [0, 0, 0, 0]for i in
range(1, 51):y
= df3
.hole
[i
]if y
>= 60 and y
<= 70:Y2
[0] += 1continueif y
> 70 and y
<= 80:Y2
[1] += 1continueif y
> 80 and y
<= 90:Y2
[2] += 1continueif y
> 90 and y
<= 100:Y2
[3] += 1continue#調用圖表生成函數
map_1(Y2
)arr
= []_mean
= [] # 平均值_var
= [] # 方差k
= ['yuwen', 'shuxue', 'yinyu', 'pthon', 'cpp']for j in
range(5):for i in
range(1, 51):arr
.append(df3
[k
[j
]][i
])#使用arr保存每一列的所有單科成績avg
= np
.mean(arr
)#求出該列的平均成績var
= np
.var(arr
)#求出該列的方差_mean
.append(avg
)#存儲五門成績各自的平均值_var
.append(var
)#存儲五門成績各自的方差#調用圖表打印函數
map_2(_mean
, _var
)
總結
以上是生活随笔為你收集整理的python 学生成绩表,生成数据表并且绘图的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。