import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import matplotlib.ticker as ticker
df = pd.read_csv("data.tsv", index_col=0 ,sep = "\t")
df = df.div(10)
fig, ax = plt.subplots(ncols=2, figsize=(7,6) )
plt.subplots_adjust(left=0.02, bottom=0.1, right=0.98, top=0.93)
plt.rcParams['font.family'] = 'sans-serif'
plt.rcParams['font.sans-serif'] = ['Noto Sans Display']
# Men
ax[0].barh(df.index, df['Men'], color='#ADD1E9', height=0.8, label='Men')
ax[0].yaxis.tick_right()
ax[1].yaxis.set_major_locator(ticker.MultipleLocator(5))
ax[0].set_xlim([130,0])
ax[0].xaxis.set_major_locator(ticker.MultipleLocator(10))
ax[0].yaxis.set_major_locator(ticker.MultipleLocator(5))
ax[0].tick_params(axis='x', labelsize=8, rotation=15)
ax[0].axes.yaxis.set_ticklabels([])
ax[0].set_axisbelow(True)
ax[0].grid(True, which='major',color='#eeeeee',linestyle='-', axis="x")
ax[0].margins(0)
#Women
ax[1].barh(df.index, df['Women'], color='#EED4D4', height=0.8, label='Women')
ax[1].set_xlim([0,130])
ax[1].xaxis.set_major_locator(ticker.MultipleLocator(10))
ax[1].yaxis.set_major_locator(ticker.MultipleLocator(5))
ax[1].tick_params(axis='x', labelsize=8, rotation=15)
ax[1].axes.yaxis.set_ticklabels([])
ax[1].set_axisbelow(True)
ax[1].grid(True, which='major',color='#eeeeee',linestyle='-', axis="x")
ax[1].margins(0)
#age / year
for i in df.index:
if i % 5 == 0 :
ax[0].text(-2.5, i-0.5, i ,fontsize=8)
ax[1].text(-15, i-0.5, "/ %d"%(2022-i) ,fontsize=8)
#Post-war 1939-1945
ax[0].axhspan(77,83, color="#eaeaea",zorder=-1)
ax[1].axhspan(77,83, color="#eaeaea",zorder=-1)
ax[0].annotate("Post-war", (120, 80) ,color="#DD1F30")
#dankai 1947-1950
ax[0].axhspan(73,76, color="#FFFD8C",zorder=-1)
ax[1].axhspan(73,76, color="#FFFD8C",zorder=-1)
ax[0].annotate("Dankai", (129, 73) ,color="#DD1F30")
#danso 1951-1960
ax[0].axhspan(62,71, color="#efffb7",zorder=-1)
ax[1].axhspan(62,71, color="#efffb7",zorder=-1)
ax[0].annotate("Danso", (120, 65) ,color="#DD1F30")
ax[0].annotate("Shin-Jinrui", (120, 57) ,color="#DD1F30")
# dankai-jr 1971-1974
ax[0].axhspan(49,52, color="#FFFD8C",zorder=-1)
ax[1].axhspan(49,52, color="#FFFD8C",zorder=-1)
ax[0].annotate("Dankai Jr.", (129, 50) ,color="#DD1F30")
# post-dankai-jr 1975-1981
ax[0].axhspan(42,48, color="#efffb7",zorder=-1)
ax[1].axhspan(42,48, color="#efffb7",zorder=-1)
ax[0].annotate("Post Dankai Jr.", (129, 43) ,color="#DD1F30")
# danso Jr 1976-1985
ax[0].annotate("Post Danso Jr.", (120, 38) ,color="#DD1F30")
# shinjinrui Jr 1986-1995
ax[0].axhspan(27,36, color="#FFFD8C",zorder=-1)
ax[1].axhspan(27,36, color="#FFFD8C",zorder=-1)
ax[0].annotate("Shin-Jinrui Jr.", (120, 30) ,color="#DD1F30")
ax[1].set_xlabel('10,000 person',loc="right")
plt.suptitle("Population of Japan by generation (2022-10)", fontsize=14)
fig.legend( facecolor="#eeeeee" , ncol=2, loc='lower center')
plt.savefig("image.svg")