Pandas+Pyecharts | 山东省高考考生数据分析可视化


大家好,我是 👉 【Python当打之年】

本期我们通过分析山东省高考考生考试成绩分布数据以及双一流大学(985/211)录取山东省考生数据,看看:

  • 山东省考生高考成绩主要集中在哪些区间
  • 本科上线率有多少
  • 双一流大学录取最低分各是多少
  • 考生报考比较多的专业有哪些

希望对小伙伴们有所帮助,如有疑问或者需要改进的地方可以私信小编。

涉及到的库:

  • Pandas — 数据处理
  • Pyecharts — 数据可视化

可视化部分:

  • 柱状图 — Bar
  • 折线图 — Line
  • 饼图 — Pie
  • 组合组件 — Grid

🏳️‍🌈 1. 导入模块

import re
import os
import pandas as pd
from pyecharts.charts import Bar
from pyecharts.charts import Line
from pyecharts.charts import Grid
from pyecharts.charts import Pie
from pyecharts import options as opts
from pyecharts.commons.utils import JsCode

🏳️‍🌈 2. Pandas数据处理

2.1 读取数据

df = pd.read_excel('./data/2020年夏季高考和普通高中学业水平等级模拟考试文化成绩一分一段表.xlsx',header=None,skiprows=[0])
df.head(10)

结果:
在这里插入图片描述

2.2 行数据填充,重置列索引,去除前2行

df.loc[0] = df.loc[0].fillna(method = 'ffill')
df.iloc[:,0] = df.iloc[:,0].fillna('')
df.columns = df.loc[0] + df.loc[1]
df = df[2:]
df.head(10)

结果:
在这里插入图片描述

🏳️‍🌈 3. Pyecharts可视化

3.1 所有考生及各学科总分分布图

colors = ["#00BCD4","#ea1d5d", "#ffb900", "#4FC3F7"]
L1 = (
    Line()
    .add_xaxis(df['分数段'])
    .add_yaxis("所有考生本段人数",df['所有考生本段人数'],symbol_size=0.5,)
    .set_series_opts(
        areastyle_opts=opts.AreaStyleOpts(opacity=1, color=colors[0]),
        label_opts=opts.LabelOpts(is_show=False),
        markarea_opts=opts.MarkAreaOpts(
            data=[
                opts.MarkAreaItem(
                    name="本科线", x=(435, 437),y=(0,2000),
                    label_opts=opts.LabelOpts(color=colors[1]),
                    itemstyle_opts=opts.ItemStyleOpts(color=colors[1])
                )
            ]
        )
    )
    .set_global_opts(
        legend_opts=opts.LegendOpts(is_show=False),
        tooltip_opts=opts.TooltipOpts(trigger="axis", axis_pointer_type="cross"),
        title_opts=opts.TitleOpts(title="所有考生总分分布",pos_bottom="93%",pos_left="center",
                                  title_textstyle_opts=opts.TextStyleOpts(color=colors[2], font_size=18),
        ),
        xaxis_opts=opts.AxisOpts(min_=df['分数段'].min(),
                                axislabel_opts=opts.LabelOpts(font_size=12, color=colors[3]),
                                axisline_opts=opts.AxisLineOpts(
                                     is_show=False,linestyle_opts=opts.LineStyleOpts(width=2, color=colors[3]))
                                ),
        yaxis_opts=opts.AxisOpts(
            axislabel_opts=opts.LabelOpts(font_size=12, color=colors[3]),
            axisline_opts=opts.AxisLineOpts(
                is_show=False,
                linestyle_opts=opts.LineStyleOpts(width=2, color=colors[3])
            ),)
    )
)
grid = Grid(init_opts=opts.InitOpts(width='1000px', height='1200px',bg_color='#0d0735'))
grid.add(L1,  grid_opts=opts.GridOpts(pos_bottom="75%", pos_top="5%"))

效果:

在这里插入图片描述

  • 所有考生的成绩基本呈正太分布,本科线附近考生数量基本靠近最大值
  • 理科(物理/化学/生物)考生的成绩分布与所有考生的成绩分布基本一致
  • 文科(政治/历史/地理)考生的成绩分布呈非正太分布,存在本科线以下的小波峰,过线率要偏低

3.2 本科上线人数比例

b1 = (
     Bar()
     .add_xaxis(df_rate_data.index.tolist()[::-1])
     .add_yaxis('', df_rate_data[0].values.tolist()[::-1], category_gap='40%')
     .set_series_opts(
         label_opts=opts.LabelOpts(
            position='insideRight',
            vertical_align='middle',
            font_size=14,
            font_weight='bold',                                                
            formatter='{c} %')
      )
      .set_global_opts(
           xaxis_opts=opts.AxisOpts(
               position='top', 
                axislabel_opts=opts.LabelOpts(font_size=16, color=colors[3]),
                axisline_opts=opts.AxisLineOpts(
                     is_show=False,linestyle_opts=opts.LineStyleOpts(width=2, color=colors[3]))
                ),
            yaxis_opts=opts.AxisOpts(
                axislabel_opts=opts.LabelOpts(font_size=16, color=colors[3]),
                axisline_opts=opts.AxisLineOpts(
                    is_show=False,
                    linestyle_opts=opts.LineStyleOpts(width=2, color=colors[3])
                ),),          

          title_opts=opts.TitleOpts(title="本科上线人数比例", pos_top="3%", pos_right="center",
                                 title_textstyle_opts=opts.TextStyleOpts(color=colors[2], font_size=20),),         
)
      .reversal_axis()
)

效果:

在这里插入图片描述

  • 本科上线考生占比 51.22%,基本一半以上的考生是过了本科线的
  • 可以看出理科考生本科上线占比要明显高于文科考生

3.3 各学科考生比例

color_series = ['#C9DA36','#37B44E','#1E91CA','#6A368B','#D5225B','#CF7B25']
df1=df.iloc[-1,[i%2==0 and i!=0 for i in range(len(df.columns))]]
subj_data = [round(i/df1.values.tolist()[-1]*100,2) for i in df1.values.tolist()][:-1]
subj_name = ['物理','化学','生物','历史','地理','政治']
df_subj = pd.DataFrame(subj_data,index=subj_name,columns=['比例'])
df_subj.sort_values('比例',ascending=False,inplace=True)
P = (
    Pie(init_opts=opts.InitOpts(width='1000px', height='600px',bg_color='#0d0735'))
    .add(
        "",
        [list(z) for z in zip(df_subj.index, df_subj['比例'])],
        radius=["30%", "70%"],
        center=["50%", "50%"],
        rosetype="radius",
        label_opts=opts.LabelOpts(is_show=False),
    )
    .set_colors(color_series)
    .set_series_opts(label_opts=opts.LabelOpts(formatter="{b}: {c}%",font_size=16))
    .set_global_opts(
        title_opts=opts.TitleOpts(title="各学科\n考生比例", pos_top="46%", pos_right="center",
                                 title_textstyle_opts=opts.TextStyleOpts(color=colors[2], font_size=28),),   
        legend_opts=opts.LegendOpts(is_show=False),
    )
)

效果:

在这里插入图片描述

  • 所有考生中选考政治科目的人数最多,占比超过2/3,达到了63.6%,选考历史的人数最少,占比为34.31%。

3.4 985/211大学(理工类)录取最低平均分数

line1 = (
    Line()
    .add_xaxis(y_score_mean)
    .add_yaxis(
        series_name = '最低平均分', 
        y_axis = x_data,
        symbol ="diamond",
        symbol_size=14,
        z=10,
        linestyle_opts=opts.LineStyleOpts(color="#FFEB3B", width=3),
        itemstyle_opts=opts.ItemStyleOpts(border_width=2, border_color="#C62828", color="#FFEB3B"),
        label_opts=opts.LabelOpts(color='#FFEB3B',position='right'),      
    )
)

bar1 = (
    Bar()
    .add_xaxis(x_data)
    .add_yaxis("2017年", y_score_2017, color='#EC407A')
    .add_yaxis("2018年", y_score_2018, color='#26A69A')
    .add_yaxis("2019年", y_score_2019, color='#3F51B5')
    .set_series_opts(
         label_opts=opts.LabelOpts(
            position='inside',
            vertical_align='middle',
         ),
      )
    .set_global_opts(
        tooltip_opts=opts.TooltipOpts(is_show=True, trigger='axis', axis_pointer_type='cross'),        
        datazoom_opts=opts.DataZoomOpts(orient="vertical",range_start=70,range_end=100),
        title_opts=opts.TitleOpts(
            title='985/211大学(理工类)录取最低分数',
            subtitle='<制图@公众号:Python当打之年>',
            pos_left='center',
            pos_top='1%',
            title_textstyle_opts=opts.TextStyleOpts(color='#ffb900', font_size=18),
        ),
        legend_opts=opts.LegendOpts(pos_left="center", pos_top='7%'),
        xaxis_opts=opts.AxisOpts(
            min_=400,
            axislabel_opts=opts.LabelOpts(font_size=14, color='#c2ff00'),
            axisline_opts=opts.AxisLineOpts(
                is_show=False,
                linestyle_opts=opts.LineStyleOpts(width=2, color='#DB7093'))),
        yaxis_opts=opts.AxisOpts(
            axislabel_opts=opts.LabelOpts(font_size=14, color='#c2ff00'),
            axisline_opts=opts.AxisLineOpts(
                is_show=False,
                linestyle_opts=opts.LineStyleOpts(width=2, color='#DB7093')
            ),
        )
    )
    .reversal_axis()    
)

效果:在这里插入图片描述

  • 985/211大学(理工类)录取平均最低分数最高的前三甲学校:北京大学(689),复旦大学(681),上海交通大学(673)
  • 中国科学技术大学(671),浙江大学(670),中国人民大学(668),南京大学(668)次之

3.5 985/211大学(理工类)录取最低平均位次

在这里插入图片描述

  • 985/211大学(理工类)录取平均最低位次最高的前三甲学校:清华大学(20),北京大学(77),复旦大学(223),上海交通大学(473)
  • 中国科学技术大学(554), 浙江大学(572),南京大学(687)紧随其后

3.6 985/211大学(文史类)录取最低平均分数

在这里插入图片描述

  • 985/211大学(文史类)录取平均最低分数最高的前三甲学校:清华大学(664),北京大学(653),复旦大学(645),
  • 中国人民大学(642),上海交通大学(641),浙江大学(639)次之

3.7 985/211大学(文史类)录取最低平均位次

在这里插入图片描述

  • 985/211大学(文史类)录取平均最低位次最高的前三甲学校:清华大学(20),北京大学(29),复旦大学(91)
  • 中华人民大学(118),上海交通大学(144),浙江大学(172)紧随其后

3.8 985/211大学录取数量前15的理工类专业

P1 = (
    Pie(init_opts=opts.InitOpts(width='1000px', height='600px',bg_color='#0d0735'))
    .add(
        "",
        [list(z) for z in zip(df_subj_top10['专业名称'].values.tolist(), df_subj_top10['数量'].values.tolist())],
        radius=["40%", "70%"],
        center=["50%", "50%"],
        label_opts=opts.LabelOpts(is_show=False),
    )
    .set_colors(color_series)
    .set_series_opts(label_opts=opts.LabelOpts(formatter="{b}: {c}",font_size=16))
    .set_global_opts(
        title_opts=opts.TitleOpts(title="录取数量前15的\n理工类专业", pos_top="46%", pos_right="center",
                                 title_textstyle_opts=opts.TextStyleOpts(color=colors[2], font_size=28),),   
        legend_opts=opts.LegendOpts(is_show=False),
    )
)

效果:
在这里插入图片描述

3.9 985/211大学录取数量前15的文史类专业

在这里插入图片描述

🏳️‍🌈 4. 可视化项目源码+数据

网盘: https://pan.baidu.com/doc/share/Olj4d~aKuXT7AF0cq01MrQ-437060019167360
提取码: pyra


以上就是本期为大家整理的全部内容了,赶快练习起来吧,原创不易,喜欢的朋友可以点赞、收藏也可以分享注明出处)让更多人知道。

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Python当打之年

您的鼓励是我创作的最大动力!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值