基于pycharts全国民政局登记数据分析可视化

import pandas as pd
from pyecharts. charts import *
from pyecharts import options as opts
from pyecharts.globals import ThemeType
from pyecharts.globals import SymbolType
from pyecharts.commons.utils import JsCode
from pyecharts.charts import Line, Bar,Page,Map

df_marry = pd.read_csv('./结婚登记(万对).csv',encoding='gbk')
df_divorce = pd.read_csv('./离婚登记(万对).csv',encoding='gbk')

2019年各地区离结率

#2019年各地区离结率
df_tmp = pd.DataFrame()
df_tmp['地区'] = df_marry['地区']
df_tmp['结婚']= df_marry['2019年']
df_tmp['离婚']= df_divorce['2019年']
df_tmp['结婚占比'] = round(df_marry['2019年']*100/(df_marry['2019年'] + df_divorce['2019年']))
df_tmp['离婚占比'] = round(df_divorce['2019年']*100/(df_marry['2019年'] + df_divorce['2019年']))
df_tmp['离结率'] = round(df_tmp['离婚占比']/(df_tmp['结婚占比']),2)
df_tmp.head()

可视化

 2019年各地区结婚离婚占比

area = df_tmp['地区'].values.tolist()
marry_count = df_tmp['结婚占比'].values.tolist()
divorce_count = df_tmp['离婚占比'].values.tolist()
c = (
    Bar(
        init_opts=opts.InitOpts(
            width='800px',
            height='600px',
        )
    )
    .add_xaxis(area)
    .add_yaxis('结婚占比',marry_count, stack='stackl',itemstyle_opts=opts.ItemStyleOpts(color='#ed1941'))
    .add_yaxis('离婚占比',divorce_count, stack='stackl',itemstyle_opts=opts.ItemStyleOpts(color='#009ad6'))
    .set_series_opts(label_opts=opts.LabelOpts(
        is_show=True,position='inside'))
    .set_global_opts(
        title_opts=opts.TitleOpts(
            title="2019年各地区结婚离婚占比",
            pos_top='2%'),
            legend_opts=opts.LegendOpts(
                pos_right='10%',
                pos_top='2%',
                orient='horizontal',
        ),
        
    
    )
    .reversal_axis()
    
)
c.render_notebook()

 列数据逆序高亮

df_tmp = df_tmp.sort_values('离结率',ascending=False)
df_tmp.style.bar(subset=[ '离结率'],color='yellow')

 离结率柱状图

sort_info = df_tmp.sort_values('离结率',ascending=True)
bl = (
    Bar(init_opts=opts.InitOpts(bg_color='white',width='800px', height='600px',))
    .add_xaxis(list(sort_info['地区'])[-20:])
    .add_yaxis('',sort_info['离结率'].values.tolist()[-20:],
             category_gap='30%',
             itemstyle_opts={
                 'normal':{
                     'shadowColor':'rgba(0, 0,0,.5)',# 阴影颜色产 
                     'shadowBlur':'5',#阴影大小
                     'shadowOffsetY': '2',# Y轴方向阴影偏移
                     'shadowOffsetX': '2',# x辆方向阴影偏移
                     'borderColor':'fff'
                     
                 }
                     }
              ) 
    .reversal_axis()
    .set_global_opts(
        xaxis_opts=opts.AxisOpts(is_show=False),
        yaxis_opts=opts.AxisOpts(is_show=False,
        axisline_opts=opts.AxisLineOpts(is_show=False),
        axistick_opts=opts.AxisTickOpts(is_show=False)
                                ),
        title_opts=opts.TitleOpts(title='离结率 TOP20',
                                 pos_left='%9',
                                 pos_top='%4',
                                 title_textstyle_opts=opts.TextStyleOpts(
                                     color='#ed1941',font_size=16)),
        visualmap_opts=opts.VisualMapOpts(
            is_show=False,
            max_=20
        ),
    )
    .set_series_opts(
        label_opts=opts.LabelOpts(position="insideLeft",font_size=10,
                                 formatter='{b}:{c}'))
)
bl.render_notebook()

北京市历年结婚/离婚登记数据

# 线性渐变
color_js0 = """new echarts.graphic.LinearGradient(0, 1, 0, 0,
    [{offset: 0, color: '#FFFFFF'}, {offset: 1, color: '#ed1941'}], false)"""

color_js1 = """new echarts.graphic.LinearGradient(0, 1, 0, 0,
    [{offset: 0, color: '#FFFFFF'}, {offset: 1, color: '#009ad6'}], false)"""


city = '北京市'
b2 = (
    Bar()
    .add_xaxis(df_divorce.columns[1:].values.tolist()[::-1])
    .add_yaxis(
        series_name="结婚登记",
        y_axis=df_marry[df_marry['地区']==city].values[0][1:].tolist()[::-1],
        markpoint_opts=opts.MarkPointOpts(
            data=[
                opts.MarkPointItem(type_="max", name="最大值"),
                opts.MarkPointItem(type_="min", name="最小值"),
            ]
        ),
        itemstyle_opts=opts.ItemStyleOpts(color=JsCode(color_js0))
    )
    .add_yaxis(
        series_name="离婚登记",
        y_axis=df_divorce[df_divorce['地区']==city].values[0][1:].tolist()[::-1],
        markpoint_opts=opts.MarkPointOpts(
            data=[
                opts.MarkPointItem(type_="max", name="最大值"),
                opts.MarkPointItem(type_="min", name="最小值"),
            ]
        ),
        itemstyle_opts=opts.ItemStyleOpts(color=JsCode(color_js1))
    )
    .set_global_opts(
        title_opts=opts.TitleOpts(title=""),
        tooltip_opts=opts.TooltipOpts(trigger="axis"),
        xaxis_opts=opts.AxisOpts(name="年份",type_="category", 
                                 boundary_gap=True,
                                 axisline_opts=opts.AxisLineOpts(is_show=True,
                                                                 linestyle_opts=opts.LineStyleOpts(width=4, color='#DB7093')),
                                 axislabel_opts=opts.LabelOpts(rotate=45)),
        yaxis_opts=opts.AxisOpts(
                axislabel_opts=opts.LabelOpts(formatter="{value} /万对"),
                name=f'{city}历年结婚/离婚登记',            
                is_scale=True,
                name_textstyle_opts=opts.TextStyleOpts(font_size=14,font_weight='bold',color='#FF1493'),
                splitline_opts=opts.SplitLineOpts(is_show=True, 
                                                  linestyle_opts=opts.LineStyleOpts(type_='dashed')),
                axisline_opts=opts.AxisLineOpts(is_show=False,
                                        linestyle_opts=opts.LineStyleOpts(width=2, color='#DB7093'))
            ),
        legend_opts=opts.LegendOpts(is_show=True, pos_top='2%', legend_icon='roundRect'),
    )
)
b2.render_notebook()

 2019年离婚率热力图

# 线性渐变
color_js = """new echarts.graphic.LinearGradient(0, 0, 1, 0,
    [{offset: 0, color: '#009ad6'}, {offset: 1, color: '#ed1941'}], false)"""
# df_tmp = df_tmp.replace('省', '', regex=True).replace('市', '', regex=True).replace('自治区', '', regex=True).replace('壮族', '', regex=True).replace('维吾尔', '', regex=True).replace('回族', '', regex=True)
map_chart = Map(init_opts=opts.InitOpts(theme='light',
                                        width='800px',
                                        height='600px'))
map_chart.add('离婚/结婚',
              [list(z) for z in zip(df_tmp['地区'].values.tolist(), df_tmp['离结率'].values.tolist())],
              maptype='china',
              is_map_symbol_show=False,
              itemstyle_opts={
                  'normal': {
                      'shadowColor': 'rgba(0, 0, 0, .5)',  # 阴影颜色
                      'shadowBlur': 5,  # 阴影大小
                      'shadowOffsetY': 0,  # Y轴方向阴影偏移
                      'shadowOffsetX': 0,  # x轴方向阴影偏移
                      'borderColor': '#fff'
                  }
              }
              )
map_chart.set_global_opts(
    visualmap_opts=opts.VisualMapOpts(
        is_show=True,
        is_piecewise=True,
        min_ = 0,
        max_ = 1,
        split_number = 5,
        series_index=0,
        pos_top='70%',
        pos_left='10%',
        range_text=['离结率', ''],
        pieces=[
            {'max':1.0, 'min':0.8, 'label':'0.8-1.0', 'color': '#990000'},
            {'max':0.8, 'min':0.6, 'label':'0.6-0.8', 'color': '#CD5C5C'},
            {'max':0.6, 'min':0.4, 'label':'0.4-0.6', 'color': '#F08080'},
            {'max':0.4, 'min':0.2, 'label':'0.2-0.4', 'color': '#FFCC99'},
            {'max':0.2, 'min':0.0, 'label':'0.0-0.2', 'color': '#FFE4E1'},
           ],
    ),
    legend_opts=opts.LegendOpts(is_show=False),
    tooltip_opts=opts.TooltipOpts(
        is_show=True,
        trigger='item',
        formatter='{b}:{c}'
    ),
    title_opts=dict(
        text='2001-2019年各地区离结率',
        left='center',
        top='5%',
        textStyle=dict(
            color='#DC143C'))
)
map_chart.render_notebook()

 

 由图可见,东北及北京、天津离结率较高,广东、海南相对较低。

  • 2
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值