2 분 소요

2. 출생아 수 및 합계 출산율

import pandas as pd
df= pd.read_excel('stat_142801.xls', skiprows=2, nrows=2, index_col=0)
df
2012 2013 2014 2015 2016 2017 2018 2019 2020
출생아 수 484.600 436.500 435.400 438.400 406.200 357.800 326.800 302.700 272.30
합계 출산율 1.297 1.187 1.205 1.239 1.172 1.052 0.977 0.918 0.84
df.index
Index(['출생아 수', '합계 출산율'], dtype='object')
df.index.values
array(['출생아\xa0수', '합계\xa0출산율'], dtype=object)
df.rename(index={'출생아\xa0수':'출생아 수', '합계\xa0출산율':'합계 출산율'}, inplace=True)
df
2012 2013 2014 2015 2016 2017 2018 2019 2020
출생아 수 484.600 436.500 435.400 438.400 406.200 357.800 326.800 302.700 272.30
합계 출산율 1.297 1.187 1.205 1.239 1.172 1.052 0.977 0.918 0.84
df.index.values
array(['출생아 수', '합계 출산율'], dtype=object)
df.loc['출생아 수'] # df.iloc[0]
2012    484.6
2013    436.5
2014    435.4
2015    438.4
2016    406.2
2017    357.8
2018    326.8
2019    302.7
2020    272.3
Name: 출생아 수, dtype: float64
df = df.T
df
출생아 수 합계 출산율
2012 484.6 1.297
2013 436.5 1.187
2014 435.4 1.205
2015 438.4 1.239
2016 406.2 1.172
2017 357.8 1.052
2018 326.8 0.977
2019 302.7 0.918
2020 272.3 0.840
import matplotlib.pyplot as plt
import matplotlib
matplotlib.rcParams['font.family'] = 'Malgun Gothic' # 글자 폰트
matplotlib.rcParams['font.size'] = 15 # 글자 크기
matplotlib.rcParams['axes.unicode_minus'] = False # 한글 폰트 사용 시, 마이너스 글자가 깨지는 현상을 해결 
plt.plot(df.index, df['출생아 수'])
plt.plot(df.index, df['합계 출산율'])
plt.show()

fig, ax1 = plt.subplots(figsize=(10, 7)) # default 값 : subplots(1,1)
ax1.plot(df.index, df['출생아 수'], color='#ff812d')

ax2 = ax1.twinx() # x 축을 공유하는 쌍둥이 axis
ax2.plot(df.index, df['합계 출산율'], color='#ffd100')

plt.show()

fig, ax1 = plt.subplots(figsize=(13, 5)) # default 값 : subplots(1,1)
ax1.set_ylabel('출생아 수 (천 명)')
ax1.set_ylim(250,700)
ax1.set_yticks([300, 400, 500, 600])
ax1.bar(df.index, df['출생아 수'], color='#ff812d')
for idx, val in enumerate(df['출생아 수']):
    ax1.text(idx, val + 12, val, ha='center')

ax2 = ax1.twinx() # x 축을 공유하는 쌍둥이 axis
ax2.set_ylabel('합계 출산율 (가임여성 1명당 명)')
ax2.set_ylim(0, 1.5)
ax2.set_yticks([0, 1])
ax2.plot(df.index, df['합계 출산율'], color='#ffd100')

plt.show()

fig, ax1 = plt.subplots(figsize=(13, 5)) # default 값 : subplots(1,1)
ax1.set_ylabel('출생아 수 (천 명)')
ax1.set_ylim(250,700)
ax1.set_yticks([300, 400, 500, 600])
ax1.bar(df.index, df['출생아 수'], color='#ff812d')
for idx, val in enumerate(df['출생아 수']):
    ax1.text(idx, val + 12, val, ha='center')

ax2 = ax1.twinx() # x 축을 공유하는 쌍둥이 axis
ax2.set_ylabel('합계 출산율 (가임여성 1명당 명)')
ax2.set_ylim(0, 1.5)
ax2.set_yticks([0, 1])
ax2.plot(df.index, df['합계 출산율'], color='#ffd100', marker='o', ms=15, lw=5, mec='w', mew=3) 
# markersize, linewidth, markeredgecolor, markeredgewidth

plt.show()

fig, ax1 = plt.subplots(figsize=(13, 5)) # default 값 : subplots(1,1)
fig.suptitle('출생아 수 및 합계출산율')
ax1.set_ylabel('출생아 수 (천 명)')
ax1.set_ylim(250,700)
ax1.set_yticks([300, 400, 500, 600])
ax1.bar(df.index, df['출생아 수'], color='#ff812d')
for idx, val in enumerate(df['출생아 수']):
    ax1.text(idx, val + 12, val, ha='center')

ax2 = ax1.twinx() # x 축을 공유하는 쌍둥이 axis
ax2.set_ylabel('합계 출산율 (가임여성 1명당 명)')
ax2.set_ylim(0, 1.5)
ax2.set_yticks([0, 1])
ax2.plot(df.index, df['합계 출산율'], color='#ffd100', marker='o', ms=15, lw=5, mec='w', mew=3) 
for idx, val in enumerate(df['합계 출산율']):
    ax2.text(idx, val + 0.08, val, ha='center' )

plt.show()

댓글남기기