In [3]:
from IPython.display import HTML

HTML('''<script>
code_show=false; 
function code_toggle() {
 if (code_show){
 $('div.input').show();
 } else {
 $('div.input').hide();
 }
 code_show = !code_show
} 
$( document ).ready(code_toggle);
</script>
<form action="javascript:code_toggle()"><input type="submit" value="Click here to toggle on/off the raw code."></form>''')
Out[3]:
In [2]:
import pandas as pd
import holoviews as hv

hv.extension('bokeh')
In [3]:
# 100% width
from IPython.core.display import display, HTML
display(HTML("<style>.container { width:100% !important; }</style>"))
In [4]:
import plotly.express as px

Hans Rosling Gapminder

TED talk

1. let's get the data

In [5]:
df = px.data.gapminder()

2. let's look at a snippet(top 10 records) of data

In [6]:
df.head(10)
Out[6]:
country continent year lifeExp pop gdpPercap iso_alpha iso_num
0 Afghanistan Asia 1952 28.801 8425333 779.445314 AFG 4
1 Afghanistan Asia 1957 30.332 9240934 820.853030 AFG 4
2 Afghanistan Asia 1962 31.997 10267083 853.100710 AFG 4
3 Afghanistan Asia 1967 34.020 11537966 836.197138 AFG 4
4 Afghanistan Asia 1972 36.088 13079460 739.981106 AFG 4
5 Afghanistan Asia 1977 38.438 14880372 786.113360 AFG 4
6 Afghanistan Asia 1982 39.854 12881816 978.011439 AFG 4
7 Afghanistan Asia 1987 40.822 13867957 852.395945 AFG 4
8 Afghanistan Asia 1992 41.674 16317921 649.341395 AFG 4
9 Afghanistan Asia 1997 41.763 22227415 635.341351 AFG 4

3. let's visualize this data

In [7]:
display('We have a total of # countries : {0}'.format(len(df.country.unique())))
'We have a total of # countries : 142'
In [8]:
countries = df.country.unique()

# we can narrow down to these countries
#countries = ['United States', 'United Kingdom', 'Japan', 'Rwanda','Guatemala', 'China', 'India', ]

df_filtered = df[df['country'].isin(countries)]

fig = px.scatter(df_filtered, x = "gdpPercap", y = "lifeExp", animation_frame = "year", animation_group = "country",
          size = "pop", color = "country", log_x = True, size_max = 45,
          range_x = [100, 100000], range_y = [25, 90], height=800
          #text = 'country'
            
)
fig.show()
In [ ]: