Perform necessary imports
from bokeh.io import output_notebook, show,curdoc
from bokeh.plotting import figure
from bokeh.models import HoverTool, ColumnDataSource,Slider,CustomJS
from bokeh.layouts import widgetbox,row
import pandas as pd
data = pd.read_csv(“gapminder_tidy.csv”)
data.set_index(‘Year’,inplace=True)
data.head()
output_notebook()
#creating ColumnDataSource
source = ColumnDataSource(data = {‘x’ : data.loc[1970].fertility.tolist(),
‘y’ : data.loc[1970].life.tolist(),
‘country’ : data.loc[1970].Country,
‘pop’ : (data.loc[1970].population / 20000000) + 2,
‘region’ : data.loc[1970].region})
#Create a figure with HoverTool
p = figure(title = ‘1970’, x_axis_label = ‘Fertility (children per woman)’,
y_axis_label=‘Life Expectancy (year)’,
plot_height = 400, plot_width = 700,
tools = [HoverTool(tooltips=‘@country’)])
#Add circle glyph to figure
p.circle(x=‘x’,y=‘y’,source=source)
Save the minimum and maximum values of the fertility column: xmin, xmax
xmin, xmax = min(data.fertility), max(data.fertility)
Save the minimum and maximum values of the life expectancy column: ymin, ymax
ymin, ymax = min(data.life), max(data.life)
Create the figure: plot
plot = figure(title=‘Gapminder Data for 1970’, plot_height=400, plot_width=700,x_range=(xmin, xmax), y_range=(ymin, ymax))
Add circle glyphs to the plot
plot.circle(x=‘x’, y=‘y’, fill_alpha=0.8, source=source)
Set the x-axis label
plot.xaxis.axis_label =‘Fertility (children per woman)’
Set the y-axis label
plot.yaxis.axis_label = ‘Life Expectancy (years)’
Add the plot to the current document and add a title
curdoc().add_root(plot)
curdoc().title = ‘Gapminder’
show(plot)
Make a list of the unique values from the region column: regions_list
regions_list = data.region.unique().tolist()
Import CategoricalColorMapper from bokeh.models and the Spectral6 palette from bokeh.palettes
from bokeh.models import CategoricalColorMapper
from bokeh.palettes import Spectral6
Make a color mapper: color_mapper
color_mapper = CategoricalColorMapper(factors=regions_list, palette=Spectral6)
Add the color mapper to the circle glyph
plot.circle(x=‘x’, y=‘y’, fill_alpha=0.8, source=source,
color=dict(field=‘region’, transform=color_mapper), legend=‘region’)
Set the legend.location attribute of the plot to ‘top_right’
plot.legend.location = ‘top_right’
Add the plot to the current document and add the title
curdoc().add_root(plot)
curdoc().title = ‘Gapminder’
show(plot)
Define the callback function for slider for Year: update_plot
def update_plot(attr, old, new):
Set the yr name to slider.value and new_data to source.data
yr = slider.value
new_data = {
‘x’ : data.loc[yr].fertility,
‘y’ : data.loc[yr].life,
‘country’ : data.loc[yr].Country,
‘pop’ : (data.loc[yr].population / 20000000) + 2,
‘region’ : data.loc[yr].region,
}
source.data = new_data
#source.trigger(‘change’)
Make a slider object: slider
slider = Slider(title=‘Year’,start=1970,end=2010,step=1,value=1970)
Attach the callback to the ‘value’ property of slider
slider.on_change(‘value’,update_plot)
Make a row layout of widgetbox(slider) and plot and add it to the current document
layout = row(widgetbox(slider), plot)
curdoc().add_root(layout)
show(layout)