import numpy as np
import matplotlib.pyplot as plt
from matplotlib import rc
fontsize=24
fontsize_small=16
rc('font', family='sans-serif', serif=['Latin Modern Roman','Palatino'], size=fontsize)
rc('legend', fontsize=fontsize)
rc('axes', labelsize=fontsize)
rc('xtick', labelsize=fontsize_small)
rc('ytick', labelsize=fontsize_small)
female_colour = "#b98acb", "#ccaad9"
male_colour = "#6ca99b", "#5fc9bb"
female_symbol = ur"$\u2640$"
male_symbol = ur"$\u2642$"
country = "kuwait"
filename = "%s-population-pyramid-2017-20170902.csv" % country
data = np.loadtxt(filename, skiprows=5, usecols=(-2,-1), delimiter="\t")
#data /=1.e3
female = data[:,0]
male = data[:,1]
legend = np.loadtxt(filename, skiprows=5, usecols=(3,), dtype=np.string_, delimiter="\t")
legend[0] = " %s " % legend[0]
legend[1] = " %s " % legend[1]
numbers = np.arange(data.shape[0])
x_max = 300
fig, ax = plt.subplots(ncols=2, sharey=True, figsize=(12,8))
plt.subplots_adjust(left=0.03, bottom=0.05, right=0.97, top=0.99, wspace=0.14, hspace=None)
ax[0].barh( numbers[0::2], female[0::2], color=female_colour[0])
ax[0].barh( numbers[1::2], female[1::2], color=female_colour[1])
ax[1].barh( numbers[0::2], male[ 0::2], color=male_colour[0])
ax[1].barh( numbers[1::2], male[ 1::2], color=male_colour[1])
ax[0].minorticks_on()
ax[1].minorticks_on()
ax[0].set(yticks=numbers+0.3, yticklabels=legend)
ax[0].yaxis.tick_right()
ax[1].yaxis.tick_left()
ax[0].xaxis.tick_bottom()
ax[1].xaxis.tick_bottom()
ax[0].tick_params(axis=u'y', which=u'both',length=0)
ax[1].tick_params(axis=u'y', which=u'both',length=0)
ax[0].set_xlim(0, x_max)
ax[1].set_xlim(0, x_max)
ax[0].invert_xaxis()
ax[0].set_ylim(numbers[0], numbers[-1]+1)
plt.text(-x_max*.692, numbers[-6]-0.8, female_symbol, axes=ax[0], ha='center', fontsize=128, color="#887788")
plt.text( x_max*.55, numbers[-6], male_symbol, axes=ax[1], ha='center', fontsize=128, color="#778877")
plt.savefig("%s-population-pyramid-2017.svg" % country)
plt.show()