Python的matplotlib庫具有很強(qiáng)大的繪圖功能,可以利用這個(gè)庫函數(shù)來進(jìn)行學(xué)生成績統(tǒng)計(jì)。假如有一個(gè)班的某科學(xué)生成績?nèi)绫?所示,可以用柱狀圖、直方圖和餅狀圖三種方式來進(jìn)行統(tǒng)計(jì)分析。下邊介紹一下操作步驟。
學(xué)號(hào) |
分?jǐn)?shù) |
20221001 |
61 |
20221002 |
68 |
20221003 |
74 |
20221004 |
70 |
20221005 |
53 |
20221006 |
66 |
20221007 |
66 |
20221008 |
71 |
20221009 |
67 |
20221010 |
84 |
20221011 |
79 |
20221012 |
73 |
20221013 |
64 |
20221014 |
90 |
20221015 |
81 |
20221016 |
82 |
20221017 |
77 |
20221018 |
51 |
20221019 |
78 |
20221020 |
75 |
20221021 |
69 |
20221022 |
83 |
20221023 |
56 |
20221024 |
73 |
20221025 |
93 |
20221026 |
63 |
20221027 |
80 |
20221028 |
76 |
20221029 |
80 |
20221030 |
74 |
20221031 |
72 |
20221032 |
88 |
20221033 |
62 |
20221034 |
85 |
20221035 |
86 |
20221036 |
58 |
20221037 |
72 |
20221038 |
72 |
20221039 |
70 |
20221040 |
91 |
目錄
一、把成績表轉(zhuǎn)成csv格式
二、統(tǒng)計(jì)分析的方法
1、柱狀圖
2、直方圖
3、餅狀圖
三、實(shí)現(xiàn)代碼
四、實(shí)現(xiàn)效果
一、把成績表轉(zhuǎn)成csv格式
學(xué)號(hào)用字段“No.”表示,成績用“score”,表格名稱為“score”。
No. | score |
20221001 | 61 |
20221002 | 68 |
20221003 | 74 |
20221004 | 70 |
20221005 | 53 |
20221006 | 66 |
20221007 | 66 |
20221008 | 71 |
20221009 | 67 |
20221010 | 84 |
20221011 | 79 |
20221012 | 73 |
20221013 | 64 |
20221014 | 90 |
20221015 | 81 |
20221016 | 82 |
20221017 | 77 |
20221018 | 51 |
20221019 | 78 |
20221020 | 75 |
20221021 | 69 |
20221022 | 83 |
20221023 | 56 |
20221024 | 73 |
20221025 | 93 |
20221026 | 63 |
20221027 | 80 |
20221028 | 76 |
20221029 | 80 |
20221030 | 74 |
20221031 | 72 |
20221032 | 88 |
20221033 | 62 |
20221034 | 85 |
20221035 | 86 |
20221036 | 58 |
20221037 | 72 |
20221038 | 72 |
20221039 | 70 |
20221040 | 91 |
二、統(tǒng)計(jì)分析的方法
在課程成績情況統(tǒng)計(jì)分析時(shí),可以選則柱狀圖、直方圖和餅狀圖三種圖形。
1、柱狀圖
使用柱狀圖分析全班平均分?jǐn)?shù)、最高分?jǐn)?shù)和最低分?jǐn)?shù)。
2、直方圖
使用直方圖和正態(tài)分布曲線分析全班成績的正態(tài)分布情況。
3、餅狀圖
使用餅狀圖分析60分以下、60-70分、70-80分、80-90分和90分以上幾個(gè)分?jǐn)?shù)區(qū)間的人數(shù)所占比例。
三、實(shí)現(xiàn)代碼
import pandas as pd # 引入panda工具集
import numpy as np # 引入numpy核心庫
import matplotlib.pyplot as plt #引入matplotlib數(shù)據(jù)可視化庫
#聲明變量
a=0 #90分以上數(shù)量
b=0 #80-90分以上數(shù)量
c=0 #70-80分以上數(shù)量
d=0 #60-70分以上數(shù)量
e=0 #60分以下數(shù)量
score_max=0
score_min=100
score_avg=0
score_sum=0
# 正態(tài)分布的概率密度函數(shù)
# x 數(shù)據(jù)集中的某一具體測量值
# mu 數(shù)據(jù)集的平均值,反映測量值分布的集中趨勢
# sigma 數(shù)據(jù)集的標(biāo)準(zhǔn)差,反映測量值分布的分散程度
def normfun(x, mu, sigma):
pdf = np.exp(-((x - mu) ** 2) / (2 * sigma ** 2)) / (sigma * np.sqrt(2 * np.pi))
return pdf
if __name__ == '__main__':
data = pd.read_csv('score.csv') # 載入分?jǐn)?shù)數(shù)據(jù)文件
score = data['score'] # 獲得分?jǐn)?shù)數(shù)據(jù)集
student_no = data['No.'] # 獲得學(xué)號(hào)數(shù)據(jù)集
mean = score.mean() # 獲得分?jǐn)?shù)數(shù)據(jù)集的平均值
std = score.std() # 獲得分?jǐn)?shù)數(shù)據(jù)集的標(biāo)準(zhǔn)差
#計(jì)算分?jǐn)?shù)總和、各分?jǐn)?shù)區(qū)間數(shù)量統(tǒng)計(jì)
for i in range(0,len(score)):
score0=int(score[i])
#print(student_no[i],score0)
score_sum=score_sum+score0 #計(jì)算分?jǐn)?shù)之和,為求平均數(shù)做準(zhǔn)備
#計(jì)算最大值
if score0>score_max:
score_max=score0
#計(jì)算最小值
if score0<score_min:
score_min=score0
if score0>=90: #統(tǒng)計(jì)90分以上數(shù)量
a=a+1
elif score0>=80: #統(tǒng)計(jì)80分以上數(shù)量
b=b+1
elif score0>=70: #統(tǒng)計(jì)70分以上數(shù)量
c=c+1
elif score0>=60: #統(tǒng)計(jì)60分以上數(shù)量
d=d+1
else: #統(tǒng)計(jì)60分以下數(shù)量
e=e+1
score_avg=score_sum/len(score) #平均分
scores=[a,b,c,d,e] #分?jǐn)?shù)區(qū)間統(tǒng)計(jì)
# 柱形圖柱形的寬度
bar_width = 0.3
# 設(shè)定X軸:前兩個(gè)數(shù)字是x軸的起止范圍,第三個(gè)數(shù)字表示步長,步長設(shè)定得越小,畫出來的正態(tài)分布曲線越平滑
x = np.arange(0, 100, 1)
# 設(shè)定Y軸,正態(tài)分布函數(shù)
y = normfun(x, mean, std)
# 設(shè)定柱狀圖x軸、Y軸數(shù)組
x3 = np.arange(3)
y3 = np.array([score_avg,score_max,score_min])
# 繪制分?jǐn)?shù)數(shù)據(jù)集的正態(tài)分布曲線和直方圖(5分檔)
plt.subplot(221)
plt.rcParams['font.sans-serif'] = ['SimHei']
plt.title('分?jǐn)?shù)分布(5檔)')
plt.plot(x, y)
plt.hist(score, bins=5, rwidth=0.9, density=True)
plt.xlabel('分?jǐn)?shù)')
plt.ylabel('概率')
# 繪制分?jǐn)?shù)數(shù)據(jù)集的正態(tài)分布曲線和直方圖(10分檔)
plt.subplot(222)
plt.rcParams['font.sans-serif'] = ['SimHei']
plt.title('分?jǐn)?shù)分布(10檔)')
plt.plot(x, y)
plt.hist(score, bins=10, rwidth=0.9, density=True)
plt.xlabel('分?jǐn)?shù)')
plt.ylabel('概論')
# 繪制柱形圖
plt.subplot(223)
plt.rcParams['font.sans-serif'] = ['SimHei']
plt.title('分?jǐn)?shù)統(tǒng)計(jì)')
plt.bar(x3, y3, tick_label=['平均分', '最高分', '最低分'], width=bar_width)
# 繪制餅狀圖
plt.subplot(224)
plt.rcParams['font.sans-serif'] = ['SimHei']
plt.title('分?jǐn)?shù)段餅圖')
plt.pie(scores,labels=['90分以上','80-90分','70-80分','60-70分','60分以下'])
# 輸出四幅圖
plt.show()
四、實(shí)現(xiàn)效果
(全文結(jié)束)文章來源:http://www.zghlxwxcb.cn/news/detail-502072.html
?文章來源地址http://www.zghlxwxcb.cn/news/detail-502072.html
到了這里,關(guān)于python柱狀圖、直方圖和餅狀圖統(tǒng)計(jì)學(xué)生成績的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!