import wbgapi as wb
import pandas as pd
import plotly.express as px
import plotly.graph_objects as go
import numpy as np
wb.series.info('NY.GDP.PCAP.CD')
NY.GDP.PCAP.CD |
GDP per capita (current US$) |
|
1 elements |
AFE |
Africa Eastern and Southern |
AFR |
Africa |
AFW |
Africa Western and Central |
ARB |
Arab World |
CAA |
Sub-Saharan Africa (IFC classification) |
CEA |
East Asia and the Pacific (IFC classification) |
CEB |
Central Europe and the Baltics |
CEU |
Europe and Central Asia (IFC classification) |
CLA |
Latin America and the Caribbean (IFC classification) |
CME |
Middle East and North Africa (IFC classification) |
CSA |
South Asia (IFC classification) |
CSS |
Caribbean small states |
EAP |
East Asia & Pacific (excluding high income) |
EAR |
Early-demographic dividend |
EAS |
East Asia & Pacific |
ECA |
Europe & Central Asia (excluding high income) |
ECS |
Europe & Central Asia |
EMU |
Euro area |
EUU |
European Union |
FCS |
Fragile and conflict affected situations |
HPC |
Heavily indebted poor countries (HIPC) |
LAC |
Latin America & Caribbean (excluding high income) |
LCN |
Latin America & Caribbean |
LDC |
Least developed countries: UN classification |
LTE |
Late-demographic dividend |
MDE |
Middle East (developing only) |
MEA |
Middle East & North Africa |
MNA |
Middle East & North Africa (excluding high income) |
NAC |
North America |
NAF |
North Africa |
NRS |
Non-resource rich Sub-Saharan Africa countries |
OED |
OECD members |
OSS |
Other small states |
PRE |
Pre-demographic dividend |
PSS |
Pacific island small states |
PST |
Post-demographic dividend |
RRS |
Resource rich Sub-Saharan Africa countries |
SAS |
South Asia |
SSA |
Sub-Saharan Africa (excluding high income) |
SSF |
Sub-Saharan Africa |
SST |
Small states |
SXZ |
Sub-Saharan Africa excluding South Africa |
WLD |
World |
XZN |
Sub-Saharan Africa excluding South Africa and Nigeria |
|
44 elements |
df = wb.data.DataFrame(
['NY.GDP.PCAP.CD'],
wb.region.members('WLD'),
skipBlanks=True,
labels=True).reset_index()
df = df.melt(id_vars=['economy', 'Country'], var_name='Year', value_name='GDP Per Capita')
df['Year'] = df['Year'].str.replace('YR', '').astype(int)
df.head()
0 |
CUB |
Cuba |
1960 |
NaN |
1 |
GNB |
Guinea-Bissau |
1960 |
NaN |
2 |
NRU |
Nauru |
1960 |
NaN |
3 |
VCT |
St. Vincent and the Grenadines |
1960 |
155.294494 |
4 |
EGY |
Egypt, Arab Rep. |
1960 |
NaN |
Turn into log scale for more apparent map
hist = px.histogram(np.log10(df[df['Year']==2022]['GDP Per Capita']))
hist.update_traces(marker=dict(color='rgb(111, 198, 107)'))
hist.show()
fig = go.Figure()
fig = go.Figure(data=go.Choropleth(
locations=df[df['Year']==2022]['economy'],
z=np.log10(df[df['Year']==2022]['GDP Per Capita']),
customdata = df[df['Year']==2022]['GDP Per Capita'],
text=df['Country'],
hovertemplate="Country: %{text} <br>%{customdata:$,.2f}<extra></extra>",
colorscale="haline",
colorbar=dict(title="GDP Per Capita (US$)",
tickvals = [2, 3, 4, 5],
ticktext = ['100', '1,000', '10,000', '100,000'],
xanchor="left"),
autocolorscale=False,
marker_line_color='darkgray'
))
fig.update_geos(fitbounds="locations", visible=False)
fig.update_layout(
title_text="World GDP Per Capita in 2022",
geo=dict(
resolution=50,
scope='world')
)
fig.show(config={
'scrollZoom': False
})
df_vn = wb.data.DataFrame(
wb.topic.members(3),
'VNM',
mrv=50,
skipBlanks=True,
labels=True).reset_index()
df_vn = df_vn.rename(columns={"series":"Code", "Series":"Indicators"})
df_vn = df_vn.melt(id_vars=['Code', 'Indicators'], var_name='Year', value_name='Value')
df_vn['Year'] = df_vn['Year'].str.replace('YR', '').astype(int)
df_vn.info()
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 11400 entries, 0 to 11399
Data columns (total 4 columns):
# Column Non-Null Count Dtype
--- ------ -------------- -----
0 Code 11400 non-null object
1 Indicators 11400 non-null object
2 Year 11400 non-null int32
3 Value 6963 non-null float64
dtypes: float64(1), int32(1), object(2)
memory usage: 311.8+ KB
default_code = df_vn['Code'].unique()[12]
select_ind=list([dict(label=ind,
method="update",
args=[{"y": [df_vn[df_vn['Code'] == code]['Value']],
"x": [df_vn[df_vn['Code'] == code]['Year']]}])
for code, ind in zip(df_vn['Code'].unique(), df_vn['Indicators'].unique())
])
select_chart=[dict(label="Bar",
method="restyle",
args=['type', 'bar']),
dict(label="Line",
method="restyle",
args=[{"type": "scatter", "mode": "lines+markers"}])
]
# Create a bar trace
bar_trace = go.Bar(
x=df_vn[df_vn['Code'] == default_code]['Year'],
y=df_vn[df_vn['Code'] == default_code]['Value']
)
# Create the layout
layout = go.Layout(
xaxis=dict(title='Year'),
yaxis=dict(title='Value')
)
# Create the figure
fig = go.Figure(data=[bar_trace], layout=layout)
fig.update_layout(
hovermode="x unified",
xaxis=dict(
rangeslider=dict(
visible=True
),
type="linear"
),
updatemenus=[
dict(
buttons=select_ind,
direction="down",
showactive=False,
active=12,
x=0.06,
xanchor="left",
y=1.2,
yanchor="top",
font = dict(size=11)
),
dict(
buttons=select_chart,
direction="down",
showactive=False,
active=0,
x=0,
xanchor="left",
y=1.2,
yanchor="top",
font = dict(size=11)
)],
template='plotly_white'
)
fig.update_traces(marker=dict(color='rgb(111, 198, 107)'))
fig.show()