# coding: utf-8 # In[ ]: import pandas as pd data = pd.read_csv('assets/gapminder.csv', thousands=',', index_col='Year') data.head() # In[ ]: from bokeh.io import output_notebook output_notebook() # ### Scatter plot of 2010 - income vs life expectancy # In[ ]: data.loc[2010].head() # In[ ]: 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) # #### [01 - plotting](01 - plotting.ipynb) # In[ ]: #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) # ### Column Data Source # In[ ]: 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 #}) # #### [02 - column data source](02 - column data source.ipynb) # Now we can show regions by color # In[ ]: regions = list(data.region.unique()) regions # In[ ]: from bokeh.palettes import Spectral6 Spectral6 # In[ ]: def get_color(r): return Spectral6[regions.index(r.region)] data['region_color'] = data.apply(get_color, axis=1) data.head() # In[ ]: #p.circle(x='income', y='life', size=20, alpha=0.6, color='color', source=source) #show(p) # ### Add a hover # In[ ]: 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) # #### [03 - interactions](03 - interactions.ipynb) # ### Examples Interlude - [A1 - Extra Resources](A1 - Extra Resources.ipynb) # #### [04 - styling](04 - styling.ipynb) # #### [05 - data transformations](05 - data transformations.ipynb) # ### Working plot # In[ ]: 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) # ### Interactivity with slider # In[ ]: 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) # In[ ]: from ipywidgets import interact, IntSlider slider = IntSlider(min=1960, max=2014, value=2010) interact(update, year=slider) # #### [06 - server](06 - server.ipynb) # #### [07 - sharing](07 - sharing.ipynb) # If we haven't talked about it yet: # - tab completing in notebook # - fuzzy search