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)

df = pd.read_csv('data/top6-source-countries-us-imports.csv',
                 comment='#')
start_yr, end_yr = df['Year'].min(), df['Year'].max()

#1
lines, labels = list(), list()
#2
for country, group in df.groupby(by='Country'):
#3
    line, = ax.plot(group['Year'], group['Imports'], marker='o')  

#4
    lines.append(line) 
    labels.append(country)

#5
ax.legend(handles=lines, labels=labels, loc='center right', 
          ncol=3, bbox_to_anchor=[1.0,0.9])

ax.set_xticks(df['Year'].unique())
ax.set_ylim(bottom=0, top=700)
ax.spines[['right', 'top']].set_visible(False)

title = (f'United States imports by source country ({start_yr}\u2012{end_yr})')
subtitle = ('Total imports in bn USD, goods only. '
            'Source: US Census Bureau.')
set_title_and_subtitle(fig=fig, title=title, subtitle=subtitle,
                       alignment='left', h_offset=35)

fig.savefig('charts/line-chart-multiple.png', bbox_inches='tight',
            dpi=300)