-
-
Save Jimmy1106/6c43bad5f12297ef89e9709e5ac77b78 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import pandas as pd | |
import matplotlib.pyplot as plt | |
url = 'https://od.cdc.gov.tw/eic/Age_County_Gender_061.csv' | |
df = pd.read_csv(url) | |
# df = pd.read_csv('Age_County_Gender_061.csv') | |
北部 = ["基隆市", "台北市", "新北市", "桃園市", "新竹縣", "新竹市", '宜蘭縣'] | |
中部 = ["苗栗縣", "台中市", "南投縣", "彰化縣", "雲林縣"] | |
南部 = ["嘉義縣", "嘉義市", "台南市", "高雄市", "屏東縣", "澎湖"] | |
東部 = ["花蓮縣", "台東縣"] | |
外島 = ["金門縣", "連江縣"] | |
# 建立「分區」欄位 | |
df['分區'] = df['縣市'] | |
df['分區'] = df['分區'].replace(to_replace=北部 , value='北部') | |
df['分區'] = df['分區'].replace(to_replace=中部 , value='中部') | |
df['分區'] = df['分區'].replace(to_replace=南部 , value='南部') | |
df['分區'] = df['分區'].replace(to_replace=東部 , value='東部') | |
df['分區'] = df['分區'].replace(to_replace=外島 , value='外島') | |
terrible_years = [2014,2015] | |
candidate_years = [2003,2004,2005,2006,2007,2008,2009,2010,2011,2012,2013] | |
df = df[df['發病年份'].isin(candidate_years)] | |
df_北 = df[df['分區']=='北部'] | |
df_中 = df[df['分區']=='中部'] | |
df_南 = df[df['分區']=='南部'] | |
北部歷年病例數 = df_北.groupby('發病年份')['確定病例數'].sum() | |
中部歷年病例數 = df_中.groupby('發病年份')['確定病例數'].sum() | |
南部歷年病例數 = df_南.groupby('發病年份')['確定病例數'].sum() | |
plt.rcParams['font.sans-serif']= ['Arial Unicode MS'] | |
plt.figure(dpi=300) | |
北部歷年病例數.plot(label='北部', marker='.', ms=12) | |
中部歷年病例數.plot(label='中部', marker='.', ms=12) | |
南部歷年病例數.plot(label='南部', marker='.', ms=12) | |
# for i in range(len(candidate_years)): | |
# plt.text( i, 北部歷年病例數.values[i], | |
# 北部歷年病例數.values[i], ha='center', fontsize=20, bbox=dict(facecolor='yellow', alpha=0.5)) | |
plt.xticks(candidate_years) | |
plt.xlabel('發病年份') | |
plt.ylabel('確定病例數') | |
plt.grid() | |
plt.legend() | |
plt.title('登革熱北中南歷年病例數(2003~2013)') | |
plt.savefig('登革熱北中南病例數折線圖(2003~2013)') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment