Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save Jimmy1106/ed09a69c0ad72539e1befccbc65c0ea5 to your computer and use it in GitHub Desktop.
Save Jimmy1106/ed09a69c0ad72539e1befccbc65c0ea5 to your computer and use it in GitHub Desktop.
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 = df[(df['發病年份']!=2014) & (df["發病年份"]!=2015)]
# print(df['發病年份'].value_counts().sort_index().index)
# 建立「分區」欄位
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='外島')
# print(df[df["分區"]=='北部'])
# print(df['分區'].value_counts())
sorted_region = ['北部', '中部', '南部', '東部' , '外島']
regionPeopleCount = df.groupby('分區')['確定病例數'].sum()
regionPeopleCount = regionPeopleCount[sorted_region]
plt.rcParams['font.sans-serif']= ['Arial Unicode MS']
plt.figure(dpi=300)
patches, texts, autotexts = plt.pie( regionPeopleCount , labels=None, #labels=regionPeopleCount.index,
autopct="%.2f%%", pctdistance=0.6)
autotexts[3].set_fontsize(0)
autotexts[4].set_fontsize(0)
plt.legend(labels=regionPeopleCount.index, fontsize=15, loc='best')
plt.title('登革熱各區域確診人數比例(排除2014、2015年)')#,fontsize=25)
plt.savefig('登革熱按行政院區域劃分圓餅圖(排除2014、2015年)')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment