개미들을 위한 퀀트 자동 매매 프로그램 개발자 블로그입니다. 프로그래밍과 퀀트 투자를 합니다.

방구석퀀트

네이버카페 : 방구석 퀀트 놀러오세요^^ https://cafe.naver.com/conerquant 가입해 주세요.

2019년 5월 22일 수요일

[ python dash ] 부동산 연령별 인구 현황 stacked bar chart 구현

이전 시간 참고 :
부동산 지역별 평균연령 bar chart 구현
https://euhyeji.blogspot.com/2019/05/python-dash-bar-chart.html )

부동산 지역별 평당 매매/전세 가격 bar chart 구현
https://euhyeji.blogspot.com/2019/05/python-dash-bar-chart_17.html )

부동산 지역별 인구수 및 세대당 인구 bar chart 구현
https://euhyeji.blogspot.com/2019/05/python-dash-bar-chart_21.html )



연령별 인구현황을 stacked bar chart로 구현해보자.

1. 원 소스 획득 ( 행정안전부 : http://27.101.213.4/index.jsp# )

연령별 인구 현황 -> 통계표 -> 조회기간 월간 선택 -> 연령구분단위 10세 선택 -> 검색 클릭 -> 전체읍면동현황 체크 -> csv 파일 다운로드

제공되는 그래프 형태는 아래와 같다.

연령대별(10세단위) 남/녀별 인구수/구성비를 조회할 수 있고, 연령계층별로도 조회 할 수 있다. 연령계층별로 구분해 놓은 15~64세 구간은 생산활동가능 인구로 구분하는 구간이다. 참고로 핵심경제활동인구 구간은 35세~55세이다.

다운로드 받은 csv 파일 내용은 아래와 같다.


챠트를 그리기 위한 모든 데이터를 포함하고 있다.

2. dash : 연령별 인구 현황 stacked bar chart

@app.callback(
    Output('GenratioGraph', 'figure'),
    [Input('category1', 'value'),
     Input('category2', 'value'),
     Input('category3', 'value')])
def update_GenratioGraph(selected_category1, selected_category2, selected_category3):
    ratio = genratio_graph[genratio_graph.columns[pd.Series(genratio_graph.columns).str.startswith('2019년04월_계')]]
    for col in ratio.columns:  # Iterate over chosen columns
        ratio[col] = ratio[col].str.replace(",","")
        ratio[col] = pd.to_numeric(ratio[col])
    ratio['70세이상'] = ratio.iloc[:,9]+ratio.iloc[:,10]+ratio.iloc[:,11]+ratio.iloc[:,12]
    ratio = ratio.div(ratio.iloc[:,1], axis=0)
    ratio = ratio.drop(ratio.iloc[:,9:13],axis=1)
    ratio = ratio.drop(ratio.iloc[:,0:2],axis=1)
    
    traces = []
    names = ['0~9세','10~19세','20~29세','30~39세',
             '40~49세','50~59세','60~69세','70세이상']
    for i in range(int(8)):
        trace0=go.Bar(
            x=genratio_graph['읍/면/동'],
            y=ratio.iloc[:,i],
            text=ratio.iloc[:,i],
            opacity=0.6,
            name = names[i]
            )
        traces.append(trace0)
    
    return {
        'data': traces,
        'layout': go.Layout(
            xaxis={
                #'title': '읍/면/동',                
            },
            yaxis=dict(
                title = '연령별 인구현황',
                tickformat='%',                
            ),
            margin={'l': 40, 'b': 100, 't': 10, 'r': 40},
            hovermode='closest',
            barmode='stack',
            height=300
        )
    }

stacked bar chart를 그리기 위해서는 layout에 barmode='stack' 이라고 설정하고, % 구성비로 표현하기 위해 yaxis에 tickformat='%' 라고 설정하면 된다.

3. 구현 결과 : geenieland.na.to

아래와 같이 지역별 연령별 인구 현황을 stcked bar chart로 확인 할 수 있다.

댓글 없음:

댓글 쓰기

가장 많이 본 글