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)

#1
df = pd.read_csv('data/eu-per-capita-gdp-by-country-2021.csv',
                 comment='#')
df['Per_capita_gdp'] = df['Per_capita_gdp']/10**3

#2
bin_width, lbound, ubound = 15.0, 0.0, 135.0
bin_edges = np.arange(start=lbound, stop=ubound, step=bin_width)
bin_edges = np.append(bin_edges, ubound)

#3
ax.hist(x=df['Per_capita_gdp'], bins=bin_edges, linewidth=1, 
        edgecolor='black')

#4
ax.set_xticks(ticks=bin_edges)
ax.set_xlabel(xlabel='Gross domestic product')
ax.set_ylabel(ylabel='Number of countries')

ax.spines[['right', 'top', 'left']].set_visible(False)

title = 'Distribution of per capita GDP in the EU (2021)'
subtitle = 'In current thousands of USD. Source: The World Bank.'
set_title_and_subtitle(fig=fig, title=title, subtitle=subtitle,
                       alignment='left', h_offset=20)

fig.savefig('charts/histogram-plot.png', bbox_inches='tight',
            dpi=300)
