使用python可视化学生分数

参考博客:https://blog.csdn.net/m0_72963799/article/details/127141536

在本篇博客中,笔者使用colab + python3的环境将考研分数进行可视化。

学生的成绩表格score.csv的格式如下:
使用python可视化学生分数_第1张图片
其中No.代表学生编号,score代表考研分数。
具体的python代码实现如下:

import pandas as pd  # 引入panda工具集
import numpy as np  # 引入numpy核心库
import matplotlib.pyplot as plt  #引入matplotlib数据可视化库
 
#声明变量
a=0     #400分以上数量
b=0     #390-400分以上数量
c=0     #380-390分以上数量
d=0     #370-380分以上数量
e=0     #370分以下数量
score_max=300
score_min=450
score_avg=0
score_sum=0
 
# 正态分布的概率密度函数
#   x      数据集中的某一具体测量值
#   mu     数据集的平均值,反映测量值分布的集中趋势
#   sigma  数据集的标准差,反映测量值分布的分散程度
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') # 载入分数数据文件
    score = data['score'] # 获得分数数据集
    student_no = data['No.'] # 获得学号数据集
    mean = score.mean() # 获得分数数据集的平均值
    std = score.std()   # 获得分数数据集的标准差
 
   #计算分数总和、各分数区间数量统计
    for i in range(0,len(score)):
        score0=int(score[i])
                
        #print(student_no[i],score0)
        score_sum=score_sum+score0  #计算分数之和,为求平均数做准备
 
        #计算最大值
        if score0>score_max:
            score_max=score0
 
        #计算最小值
        if score0<score_min:
            score_min=score0
 
        
        if score0>=400:              #统计400分以上数量
            a=a+1
        elif score0>=390:            #统计390分以上数量
            b=b+1
        elif score0>=380:            #统计380分以上数量
            c=c+1
        elif score0>=370:            #统计379分以上数量
            d=d+1
        else:                       #统计360分以下数量
            e=e+1
 
    score_avg=score_sum/len(score)  #平均分
    scores=[a,b,c,d,e]              #分数区间统计
    
    
 
    # 柱形图柱形的宽度
    bar_width = 0.3
    
 
    # 设定X轴:前两个数字是x轴的起止范围,第三个数字表示步长,步长设定得越小,画出来的正态分布曲线越平滑
    x = np.arange(370, 400, 1)
 
    # 设定Y轴,正态分布函数
    y = normfun(x, mean, std)
 
    # 设定柱状图x轴、Y轴数组
    x3 = np.arange(3)
    y3 = np.array([score_avg,score_max,score_min])
    
 
    # 绘制分数数据集的正态分布曲线和直方图(5分档)
    plt.subplot(221)
    plt.title('score(5)')
    plt.plot(x, y)
    plt.hist(score, bins=5, rwidth=0.9, density=True)
    plt.xlabel('score')
    plt.ylabel('possibiliity')
    
 
    # 绘制分数数据集的正态分布曲线和直方图(10分档)
    plt.subplot(222)
    plt.title('score(10)')
    plt.plot(x, y)   
    plt.hist(score, bins=10, rwidth=0.9, density=True)
    plt.xlabel('score')
    plt.ylabel('possibility')
 
 
    # 绘制柱形图
    plt.subplot(223)
    plt.title('score')
    plt.bar(x3, y3, tick_label=['average', 'highest', 'lowest'], width=bar_width)
 
    # 绘制饼状图
    plt.subplot(224)
    plt.title('pie chart')
    plt.pie(scores,labels=['400+','390-400','380-390','370-380','370-'])
    # 输出四幅图
plt.show()


最终实现的效果如图所示

你可能感兴趣的:(python,信息可视化,数据分析)