import pandas as pd
data = pd.read_csv('assets/gapminder.csv', thousands=',', index_col='Year')
data.head()
from bokeh.io import output_notebook
output_notebook()
data.loc[2010].head()
from bokeh.plotting import figure
#p = figure()
#p.circle(x=data.loc[2010].income, y=data.loc[2010].life)
from bokeh.io import show
#show(p)
#p = figure(
# height=400, x_axis_type='log',
# x_range=(100, 100000), y_range=(0, 100),
# title='2010', x_axis_label='Income', y_axis_label='Life expectancy'
#)
# MAKE A FUNCTION
#p.circle(x=data.loc[2010].income, y=data.loc[2010].life, color='firebrick')
#show(p)
from bokeh.models import ColumnDataSource
source = ColumnDataSource(
{
'column_1': [1, 2, 3],
'column_2': [3, 4, 5]
}
)
#source = ColumnDataSource({
# 'income': data.loc[2010].income,
# 'life': data.loc[2010].life,
# 'country': data.loc[2010].Country
#})
Now we can show regions by color
regions = list(data.region.unique())
regions
from bokeh.palettes import Spectral6
Spectral6
def get_color(r):
return Spectral6[regions.index(r.region)]
data['region_color'] = data.apply(get_color, axis=1)
data.head()
#p.circle(x='income', y='life', size=20, alpha=0.6, color='color', source=source)
#show(p)
from bokeh.models import HoverTool
#hover = HoverTool(tooltips='@country', show_arrow=False)
#p.circle(x='income', y='life', size=20, alpha=0.6, color='color', source=source)
#p.add_tools(hover)
from bokeh.models import NumeralTickFormatter
source = ColumnDataSource({
'income': data.loc[2010].income,
'life': data.loc[2010].life,
'country': data.loc[2010].Country,
'color': data.loc[2010].region_color,
'population': data.loc[2010].population
})
from bokeh.models import LinearInterpolator
size_mapper = LinearInterpolator(
x=[data.population.min(), data.population.max()],
y=[5, 50]
)
p = figure(
height=400, x_axis_type='log',
x_range=(100, 100000), y_range=(0, 100),
title='2010', x_axis_label='Income', y_axis_label='Life expectancy',
tools=[HoverTool(tooltips='@country', show_arrow=False)]
)
p.xaxis[0].formatter = NumeralTickFormatter(format="$0,")
p.circle(
x='income', y='life',
size={'field': 'population', 'transform': size_mapper},
color='color',
alpha=0.6,
source=source,
)
show(p)
from bokeh.io import push_notebook
source = ColumnDataSource({
'income': data.loc[2010].income,
'life': data.loc[2010].life,
'country': data.loc[2010].Country,
'color': data.loc[2010].region_color,
'population': data.loc[2010].population
})
def update(year):
new_data = dict(
income=data.loc[year].income,
life=data.loc[year].life,
country=data.loc[year].Country,
population=data.loc[year].population,
color=data.loc[year].region_color,
)
source.data = new_data
p.title.text = str(year)
push_notebook()
size_mapper = LinearInterpolator(
x=[data.population.min(), data.population.max()],
y=[5, 50]
)
p = figure(
height=400, x_axis_type='log',
x_range=(100, 100000), y_range=(0, 100),
title='2010', x_axis_label='Income', y_axis_label='Life expectancy',
tools=[HoverTool(tooltips='@country', show_arrow=False)]
)
p.xaxis[0].formatter = NumeralTickFormatter(format="$0,")
p.circle(
x='income', y='life',
size={'field': 'population', 'transform': size_mapper},
color='color',
alpha=0.6,
source=source,
)
show(p, notebook_handle=True)
from ipywidgets import interact, IntSlider
slider = IntSlider(min=1960, max=2014, value=2010)
interact(update, year=slider)
If we haven't talked about it yet: