Data Analysis/Environment

[Environment Data Analysis] - 1990년~2018년 세계 산림 면적 데이터 분석 & 시각화 with Python

동장군님 2021. 6. 12. 16:44

지난 30년 간 지구 산림 면적이 얼마나 변해왔는지 데이터로 분석하도록 하겠다. 데이터는 아래 WorldBank라는  곳에서 가져왔다.

 

https://data.worldbank.org/indicator/AG.LND.FRST.K2?end=2018&start=1990&view=map&year=1990 

 

Forest area (sq. km) | Data

Learn how the World Bank Group is helping countries with COVID-19 (coronavirus). Find Out

data.worldbank.org

1. 세계 총 산림 면적 추이 1990년 ~ 2018년

import pandas as pd
import plotly.express as px

data=pd.read_csv('C:/Users/banad/OneDrive/바탕 화면/동앤트/data.csv')
country=pd.read_csv('C:/Users/banad/OneDrive/바탕 화면/동앤트/country.csv')

df=data.melt(id_vars=['Country Name','Country Code','Indicator Name','Indicator Code'],var_name='Year',value_name='Forest_Area(sq.km)')
df=df[~df['Forest_Area(sq.km)'].isnull()]

country=country[['Country Code','Region','IncomeGroup']]
tb=pd.merge(left=df,right=country,how='left',left_on=df['Country Code'],right_on=country['Country Code'])

world=tb[tb['Country Name']=='World']

fig = px.line(world, x="Year", y="Forest_Area(sq.km)", title='1990~2019 World Forest Area')
fig.show()

 

2. 1990년 ~ 2018년 국가별 산림 면적 감소량 Top 10

 

28년 동안 산림 면적이 가장 감소한 국가로는 코트디부아르, 니카라과, 나이지리아, 감비아 등 빈민국으로 확인할 수 있다.

 

 

tb_18=tb[tb['Year']=='2018'][['Country Name','Forest_Area(sq.km)']]
tb_90=tb[tb['Year']=='1990'][['Country Name','Forest_Area(sq.km)']]

change=pd.merge(left=tb_18,right=tb_90,how='inner',left_on=tb_18['Country Name'],right_on=tb_90['Country Name'])
change=change.drop(['Country Name_x','Country Name_y'],axis=1)
change.columns=['Country','2018','1990']

change['pct_change']=((change['2018']/change['1990'])-1)*100
change_10=change.sort_values(by='pct_change',ascending=True).head(10)

fig = px.bar(change_10, x='Country', y='pct_change',text='pct_change',title='Top 10 pecentage change of Forest Area in last 28 years')
fig.update_traces(texttemplate='%{text:.2f}%',textposition='outside',)
fig.show()

 

3. 지도 시각화

마지막으로 연도별 산림 면적이 어떻게 변화하는지 지도로 시각화해보도록 하겠다.

 

 

tb2=tb[~tb['Country Name'].isin(['World','Upper middle income','Sub-Saharan Africa (IDA & IBRD countries)','South Asia (IDA & IBRD)',
                        'Middle East & North Africa (IDA & IBRD countries)','Latin America & the Caribbean (IDA & IBRD countries)',
                         'Europe & Central Asia (IDA & IBRD countries)','East Asia & Pacific (IDA & IBRD countries)',
                         'Sub-Saharan Africa (excluding high income)','Post-demographic dividend','Pacific island small states',
                         'Arab World','East Asia & Pacific (excluding high income)','Central Europe and the Baltics',
                             'Early-demographic dividend','Europe & Central Asia (excluding high income)','Europe & Central Asia',
                             'Fragile and conflict affected situations','East Asia & Pacific','Heavily indebted poor countries (HIPC)',
                             'IDA & IBRD total','Latin America & Caribbean (excluding high income)','Least developed countries: UN classification',
                             'Late-demographic dividend','Lower middle income','Low income','OECD members','Middle East & North Africa (excluding high income)',
                             'Low & middle income','North America','Latin America & Caribbean','Middle East & North Africa','Middle income',
                             'Pre-demographic dividend','Sub-Saharan Africa','High income','IBRD only','IDA total','IDA only'
                        ])]

tb2['Forest_Area(sq.km)']=tb2['Forest_Area(sq.km)'].astype(float)
tb2['Forest'] = ['<=0.1M' if x<=100000 else '<=0.5M' if 100000<x<=500000
                else '<=1M' if 500000<x<=1000000  else '<=3M' if 1000000<x<=3000000
                 else '<=5M' if 3000000<x<=5000000
                 else '>5M' for x in tb2['Forest_Area(sq.km)']]

fig=px.choropleth(tb2, 
              locations = 'Country Code_x',
              color="Forest", 
              animation_frame="Year",
              labels={'Forest_Area(sq.km)':'Forest Area'},
              category_orders={'Forest':['<=0.1M','<=0.5M','<=1M','<=3M','<=5M','>5M']},    
              title = 'Forest Area From 1990 - 2018',
              color_discrete_map={
                  '<=0.1M':"#6baed6",'<=0.5M':"#57a0ce",
                  '<=1M':"#4292c6",'<=3M': "#3082be",'<=5M':"#2171b5",'>5M':"#1361a9",
                                 },
             )

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()