台灣歷年人口(.csv)
世界0-14歲歷年人口(.csv)
世界15-64歲歷年人口(.csv)
世界65歲以上歷年人口(.csv)
import pandas as pd
import numpy as np
import csv
# 讀取人口數資料
data1 = pd.read_csv('population_taiwan.csv')
data2 = pd.read_csv('population_world_0-14.csv')
data3 = pd.read_csv('population_world_15-64.csv')
data4 = pd.read_csv('population_world_65up.csv')
country = ['臺灣', '日本', '南韓', '中國']
# 4個國家0-14歲的人口比例
p1 = []
# 4個國家15-64歲的人口比例
p2 = []
# 4個國家65歲以上的人口比例
p3 = []
# 臺灣
taiwan_p1 = data1.loc[1][58]
taiwan_p2 = data1.loc[2][58]
taiwan_p3 = data1.loc[3][58]
taiwan_all = taiwan_p1 + taiwan_p2 + taiwan_p3
p1.append(taiwan_p1 / taiwan_all * 100)
p2.append(taiwan_p2 / taiwan_all * 100)
p3.append(taiwan_p3 / taiwan_all * 100)
# 日本
country_p1 = data2.loc[117][58]
country_p2 = data3.loc[117][58]
country_p3 = data4.loc[117][58]
country_all = country_p1 + country_p2 + country_p3
p1.append(country_p1 / country_all * 100)
p2.append(country_p2 / country_all * 100)
p3.append(country_p3 / country_all * 100)
# 南韓
country_p1 = data2.loc[124][58]
country_p2 = data3.loc[124][58]
country_p3 = data4.loc[124][58]
country_all = country_p1 + country_p2 + country_p3
p1.append(country_p1 / country_all * 100)
p2.append(country_p2 / country_all * 100)
p3.append(country_p3 / country_all * 100)
# 中國
country_p1 = data2.loc[38][58]
country_p2 = data3.loc[38][58]
country_p3 = data4.loc[38][58]
country_all = country_p1 + country_p2 + country_p3
p1.append(country_p1 / country_all * 100)
p2.append(country_p2 / country_all * 100)
p3.append(country_p3 / country_all * 100)
# 匯入plotly套件,以便繪製視覺化圖形
from plotly.offline import download_plotlyjs, init_notebook_mode, plot, iplot
import plotly.graph_objs as go
trace1 = go.Bar(
x = country,
y = p1,
name = '0-14'
)
trace2 = go.Bar(
x = country,
y = p2,
name = '15-64'
)
trace3 = go.Bar(
x = country,
y = p3,
name = '65up'
)
data = [trace1, trace2, trace3]
layout = go.Layout(
barmode='stack'
)
fig = go.Figure(data=data, layout=layout)
iplot(fig, filename='stacked-bar')