Question: Create the basic chart for Life expectancy vs Fertility in bokeh server app Things to keep in mind: Use ColumnDataSource object with 'x', 'y', 'country',

Create the basic chart for Life expectancy vs Fertility in bokeh server app

Things to keep in mind:

Use ColumnDataSource object with 'x', 'y', 'country', 'pop' and 'region' keys set to fertility, life expectancy, country, population, and region where you only select the rows with value 1970 respectively. E.g. gap[gap['Year'] == xxx]['fertility'] where xx would be the year

Set x and y ranges, add x and y labels, height and width of plot

Add tools like hover tool

Since this is a bokeh app, dont forget curdoc statements such as curdoc().add_root(plot) & curdoc().title = 'Gapminder

Add categorical color mapping based on region. Dont forget to add legend and provide appropriate styling for legend

Add a slider for each year and update the chart based on values of that year. Remember you will need to update the your CDS for that year using gap[gap['Year'] == xxx]['fertility'] where xx would be the year

Add two dropdowns, one each for x and y axis to allow users more control to select what variable is plotted

This is the code what should it change?

from pandas import read_csv

from bokeh.plotting import figure

from bokeh.layouts import widgetbox,row

from bokeh.models import ColumnDataSource,CategoricalColorMapper

from bokeh.models.widgets import Select,Slider

from bokeh.palettes import Spectral6

from bokeh.io import curdoc, show

#import data

gap_data = read_csv(r'gapminder.csv',index_col='Year')

#setting up the date dimension

year_start = gap_data.index.values[0]

year_end = gap_data.index.values[-1]

year = 1970

#setting up hte source data

source = ColumnDataSource(data={

'x' : gap_data.loc[year].fertility,

'y' : gap_data.loc[year].life,

'region' : gap_data.loc[year].region,

})

#setting up the color for categorical data

regions_list = gap_data.region.unique().tolist()

color_mapper = CategoricalColorMapper(factors=regions_list, palette=Spectral6)

#setting up the plot

p = figure(title='Life v.s Fertility in 1970',

plot_width=700,

plot_height=500)

p.circle(x = 'x',

y = 'y',

alpha=0.8,

source=source,

color=dict(field='region', transform=color_mapper),

legend='region'

)

p.legend.location = "bottom_left"

#setting up the x_axis, y_axis dropdown

list = ['fertility', 'gdp']

x_axis = Select(

options=list,

value='fertility',

title='x value'

)

#defining the update_value function

def update_value(attr, old, new):

yr = slider.value

x = x_axis.value

p.xaxis.axis_label = x

source.data = {

'x' : gap_data.loc[yr][x],

'y' : gap_data.loc[yr].life,

'region' : gap_data.loc[yr].region,

}

p.title.text = 'Life v.s. %s in %d' % (x.capitalize(),yr)

#setting up the slider

slider = Slider(start=year_start,end=year_end,step=1,value=year,title='Year')

#making thing interactive using the update function

controls = [slider, x_axis]

for control in controls:

control.on_change('value', lambda attr, old, new: update_value(attr, old, new))

layout = row(widgetbox(slider,x_axis), p)

curdoc().add_root(layout)

curdoc().title = "Gapminder"

show(layout)

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!