Animating 60 Years of World GDP in Python

Each country’s share of world GDP from 1960 to 2020, shown as an animated world map and a bar chart race.
Data Analysis
Author
Published

January 16, 2022

I wanted to see how each country’s share of world GDP changed between 1960 and 2020. A static chart doesn’t show that well, so I made two animations: a world map coloured by GDP share, and a bar chart race of the top 15 countries.

The full notebook is on Kaggle.

The bar chart race

The x-axis is each country’s share of world GDP. The USA had about 40% in 1960 and about 25% in 2020. China grew sharply from 2007 and is now close to 20%.

The map

Lighter colours mean a lower share of world GDP, darker means higher.

GDP share by country, 1960 to 2020

How it’s built

The data is the GDP of all countries 1960-2020 dataset on Kaggle. It has a row per country per year, with a gdp_percent column for the share of world GDP.

1. Add country codes

To draw countries on a map, each row needs a standard 3-letter country code. pycountry handles most of them:

import pycountry

def alpha3code(column):
    codes = []
    for country in column:
        try:
            codes.append(pycountry.countries.get(name=country).alpha_3)
        except Exception:
            codes.append('None')
    return codes

data['CODE'] = alpha3code(data.country)

About 20 names didn’t match (United States, Russia, Iran, South Korea and so on), so I set those by hand:

data.loc[data['country'].str.contains('United States'), 'CODE'] = 'USA'
data.loc[data['country'].str.contains('Russia'), 'CODE'] = 'RUS'
data.loc[data['country'].str.contains('Iran'), 'CODE'] = 'IRN'

2. Join with a world map

geopandas used to ship a low-resolution world map, so the join is one merge on the country code:

import geopandas as gpd

world = gpd.read_file(gpd.datasets.get_path('naturalearth_lowres'))
world.columns = ['pop_est', 'continent', 'name', 'CODE', 'gdp_md_est', 'geometry']
merge_df = pd.merge(world, data, on='CODE')

gpd.datasets was removed in geopandas 1.0. On newer versions, download the Natural Earth low-res countries file and read it with gpd.read_file instead.

3. Animate the map

Matplotlib’s FuncAnimation redraws the map once per year:

from matplotlib.animation import FuncAnimation

years = np.sort(merge_df.year.unique())
fig, ax = plt.subplots(figsize=(10, 10), dpi=80)

def update_fig(year):
    merge_df[merge_df['year'] == year].plot(
        column='gdp_percent', cmap='BuPu', linewidth=0.8, ax=ax, edgecolor='0.8')
    ax.set_title('Percentage GDP year = ' + str(year))

anim = FuncAnimation(fig, update_fig, interval=150, repeat_delay=3000, frames=years)

4. Make the bar chart race

The bar_chart_race package wants one column per country and one row per year, so pivot first:

import bar_chart_race as bcr

race_df = merge_df.pivot(columns='country', index='year', values='gdp_percent') * 100

bcr.bar_chart_race(df=race_df, n_bars=15, period_length=300, figsize=(4, 4),
                   title='Top GDP percent Rank throughout years')

Most of the work is in step 1. If a country name doesn’t map to a code, that country silently drops out of the join.