import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
from mpl_ornaments.titles import set_title_and_subtitle

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)

gdp_sectors = ['Agriculture', 'Industry', 'Manufacturing', 'Services']

#1
grid_spec = {'hspace':0.5}
fig, axes = plt.subplots(nrows=len(gdp_sectors), ncols=1,  
                         gridspec_kw=grid_spec)

#2
cpalette = ['#1f77b4', '#ff7f0e', '#2ca02c', '#d62728']

#3
max_val = df[gdp_sectors].max().max()

#4
axes_gdp_sectors_and_colours = zip(axes, gdp_sectors, cpalette)
for ax, gdp_sector, colour in axes_gdp_sectors_and_colours:
    
#5
    rects = ax.bar(x=df['Country'], height=df[gdp_sector], color=colour, 
                   edgecolor='black')
#6
    ax.set_ylim([0,max_val])
    
#7
    ax.bar_label(container=rects, padding=3)    
            
    ax.spines[['top', 'right', 'left']].set_visible(False)
    ax.get_yaxis().set_visible(False)
    
#8
    ax.text(x=1.0, y=1.0, s=gdp_sector, horizontalalignment='right', 
            verticalalignment='top', transform=ax.transAxes, 
            color=colour)    
    
#9
for ax in axes.flatten()[:-1]:
    ax.set_xticklabels([])
#for idx in range(len(axes) - 1):
    #axes[idx].set_xticklabels([])
        
title = f'GDP of the G7 countries by sector in {year}'
subtitle = f'In bn USD. Source: The World Bank.'
set_title_and_subtitle(fig=fig, title=title, subtitle=subtitle, 
                       alignment='left', h_offset=55)

fig.savefig('charts/multiple-bar-chart.png', dpi=300, bbox_inches='tight')