import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
from mpl_ornaments.titles import set_title_and_subtitle

fig, ax = plt.subplots(nrows=1, ncols=1)

df = pd.read_csv('data/gdp-g7-2010-2020.csv', comment='#')

df = df[df['Year'] == (year := 2020)]
df.sort_values(by='Total', inplace=True, ascending=False)

#1
gdp_sectors = ['Agriculture', 'Industry', 'Manufacturing', 'Services']

#2
bottom = np.zeros(df['Country'].nunique())
#3
for gdp_sector in gdp_sectors:
    values = df[gdp_sector]    
    
#4
    ax.bar(x=df['Country'], height=values, bottom=bottom, 
           label=gdp_sector)
    
#5
    bottom = bottom + values

ax.legend(title='Sector')

ax.grid(visible=True, which='major', axis='y')
ax.set_axisbelow(True)

ax.spines[['top', 'right', 'left']].set_visible(False)

title = f'GDP of the G7 countries by sector in {year}'
subtitle = 'In bn USD. Source: The World Bank.'
set_title_and_subtitle(fig=fig, title=title, subtitle=subtitle, 
                       alignment='left', h_offset=25)


fig.savefig('charts/stacked-bar-chart.png', dpi=300, bbox_inches='tight')