人口金字塔
-
上傳csv檔案
from google.colab import files data = files.upload()
import pandas as pd import csv data = pd.read_csv('Taiwan2019.csv', skiprows = 1) data = data.transpose()[3:] print(data.head()) men = data.iloc[2][1:] women = data.iloc[3][1:] women = women * -1 y = list(range(0, 105, 5)) from plotly.offline import download_plotlyjs, init_notebook_mode, plot, iplot import plotly.graph_objs as go layout = go.Layout(barmode='overlay') trace1 = go.Bar(y=y, x=men, orientation='h', name='Men', hoverinfo='x', marker=dict(color='powderblue') ) trace2 = go.Bar(y=y, x=women, orientation='h', name='Women', text=-1 * women.astype('int'), hoverinfo='text', marker=dict(color='seagreen') ) data = [trace1, trace2] fig = go.Figure(data=data, layout=layout) iplot(fig, filename='bar_pyramid')
-