Skip to content

Instantly share code, notes, and snippets.

@t0mst0ne
Created March 6, 2019 02:23
Show Gist options
  • Save t0mst0ne/46c9ff90dc98a87157ed2bee7ad66d62 to your computer and use it in GitHub Desktop.
Save t0mst0ne/46c9ff90dc98a87157ed2bee7ad66d62 to your computer and use it in GitHub Desktop.
import pandas as pd
import matplotlib.pyplot as plt
from matplotlib.pyplot import figure
import numpy as np
from datetime import datetime, timedelta
from IPython.display import Image
%matplotlib inline
plt.rcParams["font.family"] = "SimHei"
figure(num=None, figsize=(8, 6), dpi=80, facecolor='w', edgecolor='k')
pd.set_option('display.max_columns', 100)
plt.style.use('https://gist.githubusercontent.com/rhiever/d0a7332fe0beebfdc3d5/raw/205e477cf231330fe2f265070f7c37982fd3130c/tableau10.mplstyle')
for y in range (0,12):
year = 2018 - y
df = pd.read_excel('https://www.ris.gov.tw/info-popudata/app/awFastDownload/file/y1s10-00000.xls/y1s10/00000/',sheet_name=y)
df = df[df[ df.columns[1]].str.contains('花蓮市|鳳林鎮|玉里鎮|新城鄉|吉安鄉|壽豐鄉|光復鄉|豐濱鄉|瑞穗鄉|富里鄉|秀林鄉|萬榮鄉|卓溪鄉',na=False)]
df.columns = col_list
print('Y =', y)
for c in df['區域'].unique():
df_HL = df[df['區域'].str.contains(c)]
df_HL
del df_HL['區域']
del df_HL['總計']
df_HL = df_HL.set_index('性別').T
df_HL['男+女'] = df_HL['男'] + df_HL['女']
df_HL = df_HL.reset_index()
for (i, row) in df_HL.iterrows():
plt.bar( [i, i], [row['女'], -row['男']] ,color=['#CC6699', '#008AB8'], width=0.8, align='center', edgecolor='none')
plt.xlabel('年齡')
plt.ylabel('人口數')
plt.xlim(-1, 101)
plt.ylim(-1000, 1000)
plt.yticks(np.arange(-12e2, 13e2, 2e2),['{}'.format(int(abs(x) / 1)) if x != 0 else 0 for x in np.arange(-12e2, 13e2, 2e2)])
plt.title('{}年 {}'.format(year, c))
plt.savefig('{}_{}.png'.format(year, c))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment