-
지난 10년간 대한민국 시도별 온도/강수량 데이터 분석 & 시각화 With PythonData Analysis/Environment 2021. 3. 9. 23:10
0. Data Source
KOSIS국가데이터통계포털에서 지난 10년간 시도별 온도 및 강수량 데이터를 가져와서 분석과 시각화를 시도해보도록 하겠다. (세종시는 20년 데이터만 존재하므로 이번 분서에서는 제외하도록 하겠다.)
import pandas as pd a=pd.read_csv('C:/Users/banad/Downloads/기온.csv',encoding='cp949') b=pd.read_csv('C:/Users/banad/Downloads/강수량.csv',encoding='cp949') df=pd.merge(left=a,right=b,how='inner',left_on=['행정구역별(1)','시점'],right_on=['행정구역별','시점']) df=df.drop(['행정구역별(1)'],axis=1) df.columns=['year','temp','region','preci'] df=df[df['region']!='전국(평균)'] df['temp']=df['temp'].astype(float) df['preci']=df['preci'].astype(float) df=df[df['region']!='세종특별자치시']
Output:
위 쿼리가 문제 없다면 아래와 같이 연도, 온도, 지역, 강수량 데이터를 확인할 수 있을 것이다.
1. 연도별 강수량 & 온도 비교 with Scatter Plot and Bar Chart
강수량과 온도를 한 그래프에 그리고 싶어서 Scatter과 Bar 두 가지 방식으로 시각화를 해봤다. 연도별로 데이터 수치를 볼 수 있도록 animation_frame을 추가했다.
si_do={'강원도':'Gangwon-do','경기도':'Gyeonggi-do', '경상남도':'Gyeongsangnam-do','경상북도':'Gyeongsangbuk-do', '광주광역시':'Gwangju','대구광역시':'Daegu','대전광역시':'Daejeon','부산광역시':'Busan', '서울특별시':'Seoul','세종특별자치시':'Sejongsi','울산광역시':'Ulsan','인천광역시':'Incheon', '전라남도':'Jeollanam-do','전라북도':'Jeollabuk-do','제주특별자치도':'Jeju-do','충청남도':'Chungcheongnam-do', '충청북도':'Chungcheongbuk-do'} eng_sido=pd.DataFrame.from_dict(si_do,orient='index').reset_index() eng_sido.columns=['kor','eng'] df2=pd.merge(left=df,right=eng_sido,how='left',left_on=df['region'],right_on=eng_sido['kor']) sido_dict=df2[['kor','eng']].set_index('kor') import plotly.express as px fig = px.scatter(df2, x="temp", y="preci", color="eng", animation_frame='year') fig.update_xaxes(range=[8, 18]) fig.show() f1=fig.to_json() for x,y in zip(list(sido_dict.index), list(sido_dict['eng'])): f1=f1.replace(y,x)
import plotly.express as px fig = px.bar(df2, x='eng', y='preci',color='temp',animation_frame='year',height=800) fig.show() a1=fig.to_json() for x,y in zip(list(sido_dict.index), list(sido_dict['eng'])): a1=a1.replace(y,x)
2. 연도별 강수량 & 온도 지도 시각화
파일 용량이 커서 인터랙티브 이미지 업로드가 불가해서 아래 코드와 이미지만 올리도록 하겠다. 코드가 문제 없다면 연도별로 시도 온도/강수량 데이터를 아래와 같이 지도에서 확인이 가능하다.
from urllib.request import urlopen import json with urlopen('https://raw.githubusercontent.com/southkorea/southkorea-maps/master/kostat/2018/json/skorea-provinces-2018-geo.json') as response: kor = json.load(response) max_count = df['temp'].max() fig = px.choropleth_mapbox(df, geojson=kor, locations='region', color='temp', color_continuous_scale="Viridis", range_color=(0, max_count), featureidkey="properties.name", mapbox_style="carto-positron", opacity=0.5, center = {"lat": 36.910968, "lon": 127.964387}, zoom=5, animation_frame='year' ) fig.update_geos(fitbounds="locations",visible=False) fig.update_layout(margin={"r":0,"t":0,"l":0,"b":0},) fig.show() map_temp=fig.to_json()
max_count = df['preci'].max() fig = px.choropleth_mapbox(df, geojson=kor, locations='region', color='preci', color_continuous_scale="Viridis", range_color=(0, max_count), featureidkey="properties.name", mapbox_style="carto-positron", opacity=0.5, center = {"lat": 36.910968, "lon": 127.964387}, zoom=5, animation_frame='year' ) fig.update_geos(fitbounds="locations",visible=False) fig.update_layout(margin={"r":0,"t":0,"l":0,"b":0},) fig.show() fig.write_html('map_preci.html')
3. 지난 10년간 시도별 총 강수량 트리맵으로 확인하기
아래 사각형 크기가 지난 10년간 시도별 총 강수량이라고 볼 수 있는데 워낙 영토가 좁아서 시도별로 크게 차이가 나지 않은 점을 확인할 수 있다.
'Data Analysis > Environment' 카테고리의 다른 글
전국 도시 공원 데이터 지도로 시각화 With Python, Mapbox (0) 2021.03.13 한국지형/곤충 분포도 3d 웹맵 Qgis &Qgisthreejs.js (0) 2021.03.10 강원도 춘천시 지형/토심/경사 Analysis 3d 웹맵 Qgis/Qgisthreejs.js (0) 2021.03.05 강원도 고성 지형/ASPECT Analysis 3d 웹맵 Qgis&Qgisthreejs.js (0) 2021.03.03 전국 시도별 총 가정생활 폐기물 데이터 분석 & 시각화 with Python, Mapbox, Plotly (0) 2021.03.02