-
[Environmental Data Analysis] 세계 기온 변화 데이터 분석 & 시각화 with PythonData Analysis/Environment 2021. 6. 11. 23:28
지난 국내 기온 변화 데이터에 이어서 이번에는 세계 기온 변화 현황을 한번 분석해보고자 한다. 100% 정확한 데이터셋인지는 확실히 모르겠지만 아래 캐글에서 61년부터 19년까지의 국가/대륙별 기온 변화 데이터가 있어서 한번 활용하도록 하겠다. 아래 데이터 분석 결과를 100% 믿기보다는 파이썬으로 이런 식의 분석이 가능하다 정도로 참고하면 좋을 듯싶다.
https://www.kaggle.com/sevgisarac/temperature-change
0. 데이터 가져오기 및 전처리
import pandas as pd import plotly.graph_objects as go df=pd.read_csv('D:/Download/Environment_Temperature_change_E_All_Data.csv',encoding='ISO-8859-1') df.set_index('Months', inplace=True) df.rename({'Dec\x96Jan\x96Feb': 'Winter', 'Mar\x96Apr\x96May': 'Spring', 'Jun\x96Jul\x96Aug':'Summer','Sep\x96Oct\x96Nov':'Fall'}, axis='index',inplace = True) df.reset_index(inplace = True) df=df[df['Element']=='Temperature change'] df.drop(['Months Code','Element Code','Element','Unit'],axis=1, inplace=True) df=df.melt(id_vars=['Area','Area Code','Months'],var_name='Year',value_name='temp_change')
위 코드가 문제없다면 아래와 같이 국가, 월, 연도별 기온 변화 데이터를 확인할 수 있을 것이다.
1. 지난 10년간 기온이 가장 많이 상승한 국가 Top 10
지난 10년간 기온이 가장 많이 상승한 지역은 스발바르 얀마옌 제도로 무려 3.25도 증가하였다. 노르웨이 위, 그린란드에서 동쪽 북극과 근접한 섬으로 기후 변화 위기에 가장 노출되어 있는 지역이라고 볼 수 있다.
그다음으로는 바레인과 쿠웨이트 중동 지역의 기온이 상승하였고, 그 외로 러시아, 슬로베니아, 벨라루스, 오스트리아 등 동유럽 국가 지역에서 기온이 가장 많이 상승하였다. 추운 지역의 기온이 상승하고 있다는 점을 추정해볼 수 있을 것 같다.
df_10=df.copy() df_10.set_index('Year',inplace=True) df_10=df_10.loc[[str(x) for x in range(2010,2020)]] df_10.reset_index(inplace=True) df_10_country=df_10[df_10['Area Code']<5000] df_10_change=pd.DataFrame(df_10_country.groupby(['Area']).mean()['temp_change']) df_10_change.reset_index(inplace=True) df_top_10=df_10_change.sort_values(by='temp_change',ascending=False).head(10) import plotly.express as px fig = px.bar(df_top_10, x='Area', y='temp_change',text='temp_change',title='Top Ten Countries with highest temperature change in last ten years') fig.update_traces(texttemplate='%{text:.2f}', textposition='outside') fig.update_xaxes( tickangle = 10, title_text = "Countries", title_font = {"size": 15}, title_standoff = 0) fig.update_yaxes(showticklabels=False, title='Temperature Change') fig.show()
2. 지난 10년간 대륙별 기온 상승폭
다음으로는 대륙별 지난 10년간 평균 기온 상승폭을 확인해보도록 하겠다. 확인 결과 유럽, 북미, 아프리카, 아시아, 남미, 중미, 오세아니아 순으로 높게 나타났다. 산업화를 이끈 주역이자 기후 변화에 가장 큰 책임을 지고 있다고 해도 무방한 유럽과 북미가 세계 평균 대비 높은 기온 상승폭을 보였다.
df_con=df_10[df_10['Area'].isin(['Africa','Northern America','Central America','South America','Asia','Europe','Oceania','World'])] df_con_change=pd.DataFrame(df_con.groupby(['Area']).mean()['temp_change']) df_con_change.reset_index(inplace=True) df_con_10=df_con_change.sort_values(by='temp_change',ascending=False) colors = ['lightslategray',] * len(df_con_10) colors[df_con_10['Area'].values.tolist().index('World')] = 'crimson' fig = go.Figure(data=[go.Bar( x=df_con_10['Area'], y=df_con_10['temp_change'], marker_color=colors, text=df_con_10['temp_change'] )]) fig.update_traces(texttemplate='%{text:.2f}',textposition='outside',) fig.update_layout(title_text='Temperature Change in last decade') fig.show()
3. 1961~2019 월별 기온 상승폭
지난 60년간 월별 기온 변화가 어떤지 방사형 차트로 한번 알아보도록 하겠다.
아래 그래프 재생 버튼을 클릭하면 연도, 월별 기온이 얼마나 상승 하락했는지 확인할 수 있다. 확실한 것은 21세기 들어서 방사형 폭이 굉장히 넓어지는 점을 볼 수 있다. 최근 들어 기온 상승폭이 봄, 여름, 가을, 겨울 가리지 않고 모두 크게 상승하고 있는 중이라고 해석할 수 있을 것 같다.
df0=df[df['Area']=='World'] df0.set_index("Months", inplace=True) df0 = df0.loc[['January', 'February', 'March', 'April', 'May', 'June', 'July','August', 'September', 'October', 'November', 'December' ]] df0.reset_index(inplace = True) fig = px.line_polar(df0, r=df0.temp_change, theta=df0.Months,animation_frame='Year', line_close=True) fig.update_layout( polar=dict( radialaxis=dict( visible=True, range=[-0.5, 3] )), autosize=False, width=1000, height=600, margin=dict( l=50, r=50, b=100, t=100, pad=4 ), template='seaborn', paper_bgcolor="rgb(234, 234, 242)", legend=dict( orientation="h", yanchor="bottom", y=1.02, xanchor="right", x=1 )) fig.show()
마지막으로 데이터 시각화에 하이라이트라고 볼 수 있는 지도 시각화를 해보도록 하겠다. 지난 60년간 국가별로 기온 변화가 얼마나 심각한지 지도로 한번 확인해보시기 바란다. 끝~
df_map = df.copy() # do not lose 'df', I made copy of it df_map = df_map[df_map['Months'] == 'Meteorological year'] # chose yearly base data df_map['°C'] = ['<=-1.5' if x<=(-1.5) else '<=-1.0' if (-1.5)<x<=(-1.0) else '<=0.0' if (-1.0)<x<=0.0 else '<=0.5' if 0.0<x<=0.5 else '<=1.5' if 0.5<x<=1.5 else '>1.5' if 1.5<=x<10 else 'None' for x in df_map['temp_change']] df_map['Area']=df_map['Area'].str.replace('Republic of Korea','Korea, South').replace('United States of America','United States').replace('Russian Federation','Russia') code=pd.read_csv('D:/Download/plotly_country_code.csv') code=code.drop('GDP (BILLIONS)',axis=1) tb=pd.merge(left=df_map, right=code, how='left',left_on=df_map.Area, right_on=code.COUNTRY) fig=px.choropleth(tb, locations = 'CODE', color="temp_change", animation_frame="Year", labels={'tem_change':'The Temperature Change', '°C':'°C'}, category_orders={'°C':['<=-1.5','<=-1.0','<=0.0','<=0.5','<=1.5','>1.5','None']}, color_discrete_map={'<=-1.5':"#08519c",'<=-1.0':"#9ecae1",'<=0.0':"#eff3ff",'<=0.5':"#ffffb2",'<=1.5': "#fd8d3c",'>1.5':"#bd0026",'None':"#252525"}, title = 'Temperature Change From 1961 - 2019' ) fig.update_layout( autosize=False, width=800, height=600, margin=dict( l=50, r=50, b=100, t=100, pad=4 ), template='seaborn', paper_bgcolor="rgb(234, 234, 242)", legend=dict( orientation="v", yanchor="auto", y=1.02, xanchor="right", x=1 )) fig.show()
'Data Analysis > Environment' 카테고리의 다른 글