GDP per capita and growth
Tags: #worldbank #opendata
Author: Jeremy Ravenel
Objective : allows to visualize a map of GDP per capita and the GDP growth of all the countries in the world. Click on the country on the map or select it to see the details info.
Data :
  • GDP per capita (current US$)
  • GDP growh (annual %)
Source: World Bank national accounts data.

Input

Import library

1
import pandas as pd
2
import numpy as np
3
import plotly.graph_objects as go
4
from pandas_datareader import wb
5
from naas_drivers import plotly
Copied!

Model

Get data from World Bank

1
indicators = wb.download(indicator=['NY.GDP.PCAP.CD', 'NY.GDP.PCAP.KD.ZG'], country='all', start=2013, end=2021)
2
3
indicators = indicators.reset_index()
4
indicators = indicators[['country', 'year', 'NY.GDP.PCAP.CD', 'NY.GDP.PCAP.KD.ZG']]
5
indicators.columns = ['country', 'year', 'GDP_PER_CAPITAL', 'GDP_GROWTH_PER_CAPITAL']
6
7
indicators = indicators.fillna(0)
8
9
countries = wb.get_countries()
10
countries = countries[['name', 'region', 'iso3c']]
11
12
master_table = pd.merge(indicators, countries, left_on='country', right_on='name')
13
14
master_table = master_table[master_table['region'] != 'Aggregates']
15
16
master_table = master_table.drop(columns=['name'])
17
18
master_table = master_table.dropna()
19
20
# Création de l'ensemble final
21
xls_formatted = pd.DataFrame(columns=['COUNTRY', 'YEAR', 'GDP_PER_CAPITAL', 'GDP_GROWTH_PER_CAPITAL', 'REGION', 'ISO3C'])
22
23
for index, line in master_table.iterrows():
24
xls_formatted = xls_formatted.append(
25
{
26
'COUNTRY': line['country'],
27
'YEAR': line['year'],
28
'GDP_PER_CAPITAL': line['GDP_PER_CAPITAL'],
29
'GDP_GROWTH_PER_CAPITAL': line['GDP_GROWTH_PER_CAPITAL'],
30
'REGION': line['region'],
31
'ISO3C': line['iso3c'],
32
}, ignore_index=True
33
)
34
35
master_table = xls_formatted
36
37
master_table
Copied!

Choose the year to display

1
year = "2019"
Copied!

Create mapchart

1
master_year_table = master_table[master_table['YEAR'] == year]
2
3
GDP_GROWTH_PER_CAPITAL = "GDP growth per capita"
4
GDP_PER_CAPITAL = "GDP per capita"
5
6
fig = go.Figure()
7
8
fig.layout = go.Layout(
9
#width=500,
10
#height=300,
11
annotations=[
12
go.layout.Annotation(
13
showarrow=False,
14
text='Source: World Bank',
15
xanchor='right',
16
x=1,
17
yanchor='top',
18
y=-0.05
19
)])
20
21
fig.add_trace(go.Choropleth(
22
locations=master_year_table['ISO3C'],
23
z = master_year_table['GDP_PER_CAPITAL'],
24
colorscale = [(0,"#053D8B"),(0.25,"#1164B0"),(0.5,"#298BC8"),(0.75,"#3FB7DB"), (1,"#5CD5E8")],
25
colorbar_title = "Value",
26
customdata = master_year_table['COUNTRY'],
27
hovertemplate = '<b>%{customdata}: %{z:,.0f}</b><extra></extra>'
28
))
29
30
fig.add_trace(go.Choropleth(
31
locations=master_year_table['ISO3C'],
32
visible= False,
33
z = master_year_table['GDP_GROWTH_PER_CAPITAL'],
34
colorscale = [(0,"#053D8B"),(0.25,"#1164B0"),(0.5,"#298BC8"),(0.75,"#3FB7DB"), (1,"#5CD5E8")],
35
colorbar_title = "Growth ",
36
customdata = master_year_table['COUNTRY'],
37
hovertemplate = '<b>%{customdata}: %{z:0.2f}%</b><extra></extra>'
38
))
39
40
fig.update_layout(
41
autosize=True,
42
width= 900,
43
#height= 900,
44
title=f"GDP per capital in {year}",
45
title_x=0.5,
46
title_y=0.95,
47
updatemenus=[
48
dict(
49
type = "buttons",
50
active=0,
51
direction = "left",
52
buttons=list([
53
dict(
54
args=[{"visible": [True, False]}, {"title": f"{GDP_PER_CAPITAL} in {year}"}],
55
label=GDP_PER_CAPITAL,
56
method="update"
57
),
58
dict(
59
args=[{"visible": [False, True]}, {"title": f"{GDP_GROWTH_PER_CAPITAL} in {year}"}],
60
label=GDP_GROWTH_PER_CAPITAL,
61
method="update"
62
)
63
]),
64
showactive=True,
65
x=0.32,
66
xanchor="left",
67
y=1.2,
68
yanchor="top"
69
),
70
]
71
)
72
73
fig.show()
Copied!

Output

Export chart

1
plotly.export(fig, "GDP.png", css=None)
2
plotly.export(fig, "GDP.html", css=None)
Copied!
Copy link
Edit on GitHub