import matplotlib.pyplot as plt
import pandas as pd
from mpl_ornaments.titles import set_title_and_subtitle

fig, ax = plt.subplots(nrows = 1, ncols = 1)

#1
df = pd.read_csv('data/uk-house-of-commons-2022.csv', comment='#')

#2
df['labels'] = (df['Abbreviation'] + ' ' + df['Seats'].map('{:d}'.format))
    
textprops = {'ha':'center', 'va':'center'}
wedgeprops = {'linewidth':1.5, 'edgecolor':'white', 'width':0.4}

#3
wedges, text = ax.pie(x=df['Seats'], labels=df['labels'].to_list(), 
                      colors=df['Colour'], wedgeprops=wedgeprops, 
                      textprops=textprops, labeldistance=1.25)

#4
wedges[-1].set_visible(False); text[-1].set_visible(False)

#5
df = df[df['Party'] != 'Total']
legend = fig.legend(labels=df['Party'], loc='center', 
                    bbox_to_anchor=(0.5, 0.35), ncol=3, frameon=False)

title = f'Composition of the House of Commons, UK, 2022'
subtitle = 'Source: UK Parliament.'
set_title_and_subtitle(fig=fig, title=title, subtitle=subtitle, 
                       alignment='left', h_offset=25, v_offset=15)

fig.savefig('charts/semi-doughnut-chart.png', dpi=300,
            bbox_inches='tight')