InĀ [1]:
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
# import getDataFile from parent helpers folder
import sys
sys.path.append('..')
import helpers.ttpy as helpers
InĀ [2]:
democracies = helpers.getDataFile(date="2024-11-05", filename="democracy_data.csv")
democracies.columns
Out[2]:
Index(['country_name', 'country_code', 'year', 'regime_category_index',
'regime_category', 'is_monarchy', 'is_commonwealth', 'monarch_name',
'monarch_accession_year', 'monarch_birthyear', 'is_female_monarch',
'is_democracy', 'is_presidential', 'president_name',
'president_accesion_year', 'president_birthyear', 'is_interim_phase',
'is_female_president', 'is_colony', 'colony_of',
'colony_administrated_by', 'is_communist', 'has_regime_change_lag',
'spatial_democracy', 'parliament_chambers', 'has_proportional_voting',
'election_system', 'lower_house_members', 'upper_house_members',
'third_house_members', 'has_new_constitution', 'has_full_suffrage',
'suffrage_restriction', 'electoral_category_index',
'electoral_category', 'spatial_electoral', 'has_alternation',
'is_multiparty', 'has_free_and_fair_election',
'parliamentary_election_year', 'election_month', 'election_year',
'has_postponed_election'],
dtype='object')
InĀ [3]:
populations = helpers.getDataFile("population.csv", url = "https://raw.githubusercontent.com/datasets/population/refs/heads/main/data/population.csv")
populations.columns
Out[3]:
Index(['Country Name', 'Country Code', 'Year', 'Value'], dtype='object')
InĀ [4]:
# Merge the two files on the country code and year
democracypop = pd.merge(democracies, populations, left_on=['country_code', 'year'],
right_on=['Country Code', 'Year'], how='inner')
popsum = (democracypop.groupby(['year','is_democracy'])['Value'].sum()).to_frame().reset_index()
# Create the graph
plt.figure(figsize=(16, 8))
plt.style.use('dark_background')
custom_palette = {False: 'teal', True: 'lime'}
lineplt = sns.lineplot(data=popsum, x='year', y='Value', hue='is_democracy',
palette=custom_palette, linewidth = 4, hue_order=[True, False])
lineplt.set_title("Global Population by Democracy Status", fontsize=30)
lineplt.set_ylabel("Population (in billions)", fontsize=18)
lineplt.set_xlabel("Year", fontsize=18)
lineplt.axvline(1998, color="#666")
lineplt.axvline(1984, color="#666")
lineplt.axvline(2007, color="#666")
plt.text(1998, popsum['Value'].max()/4, '1998: Indonesia and \nNigeria switch', rotation=45, verticalalignment='bottom', color='#666', fontsize=18)
plt.text(1984, popsum['Value'].max()/4, '1984: Brazil and \nUruguay switch', rotation=45, verticalalignment='bottom', color='#666', fontsize=18)
plt.text(2007, popsum['Value'].max()/4,' 2007: Thailand alternates, \nPakistan switches', rotation=45, verticalalignment='bottom', color='#666', fontsize=18)
plt.xticks(fontsize=14)
plt.yticks(fontsize=14)
plt.legend(title='Democratic Status', fontsize = 18, title_fontsize = 14)
plt.figtext(0.99, 0.04, '@paulusm@scholar.social #TidyTuesday',
horizontalalignment='right', fontsize=14, alpha=0.7, fontstyle='italic')
plt.figtext(0.99, 0.01, 'Data : BjĆørnskov & Rode (2020; democracies); World Bank (populations)',
horizontalalignment='right', fontsize=14, alpha=0.7 , fontstyle='italic')
plt.show()
C:\Users\pd2-matthews\AppData\Local\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning: use_inf_as_na option is deprecated and will be removed in a future version. Convert inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):
C:\Users\pd2-matthews\AppData\Local\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning: use_inf_as_na option is deprecated and will be removed in a future version. Convert inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):
InĀ [5]:
# Identify significant events in the data
findcontributionA = democracypop[((democracypop['year'] == 2007) & (democracypop['is_democracy'] == False))]
findcontributionB = democracypop[((democracypop['year'] == 2008) & (democracypop['is_democracy'] == True))]
findcontribution = pd.merge(findcontributionA, findcontributionB, on='country_code', how='inner')
findcontribution = findcontribution[['country_name_x', 'year_x', 'is_democracy_x', 'Value_x', 'year_y', 'is_democracy_y', 'Value_y']]
findcontribution
Out[5]:
| country_name_x | year_x | is_democracy_x | Value_x | year_y | is_democracy_y | Value_y | |
|---|---|---|---|---|---|---|---|
| 0 | Bhutan | 2007 | False | 681614 | 2008 | True | 689737 |
| 1 | Malaysia | 2007 | False | 27092604 | 2008 | True | 27664296 |
| 2 | Nepal | 2007 | False | 26713655 | 2008 | True | 26881544 |
| 3 | Pakistan | 2007 | False | 181924521 | 2008 | True | 185931955 |
| 4 | Thailand | 2007 | False | 66826754 | 2008 | True | 67328239 |