#!/usr/bin/python # -*- coding: UTF-8 -*- from bokeh.plotting import figure, output_file, show import pandas as pd import numpy as np gapminder_filepath = "https://titlon.uit.no/hht/data/gapminder.csv" g = pd.read_csv(gapminder_filepath)#reading data #creating figure: p = figure( title = "", x_axis_type="log", x_axis_label='BNP per innbygger (NOK)', y_axis_label='Forventet antall leveår', tools="hover", tooltips=[ ("Land","@country"), ("BNP/innbygger","@gdp_capNOK"), ("Forventet alder","@life_exp_rnd"), ("Befolkning","@pop_mill_str") ], plot_height=580, plot_width=980) #creating a dictionary of continents with norwegian translation: continents={'Africa': 'Afrika', 'Americas': 'Sør/Nord-Amerika', 'Asia': 'Asia', 'Europe': 'Europa', 'Oceania': 'Oseania'} #assigning a color hex-code to each continent colors={'Africa': '#E14827', 'Americas': '#84E127', 'Asia': '#2792E1', 'Europe': '#BC27E1', 'Oceania': '#E04A6C'} #setting the info displayed when hoovering the plot g['colors']=g['continent'].apply(lambda cont: colors[cont]) g['size']=g['population'].apply(lambda pop: pop**0.5/300) g['life_exp_rnd']=g['life_exp'].apply(lambda l: int(l)) g['pop_mill_str']=g['population'].apply(lambda pop: '{:,}'.format(int(pop/1000000))) g['gdp_capNOK']=g['gdp_cap'].apply(lambda gdp: '{:,}'.format(int(8.9*gdp)).replace(',',' ')) g['continent_no']=g['continent'].apply(lambda cont: continents[cont]) #obtaining a scatterplot: p.scatter(x = 'gdp_cap', y = 'life_exp', size='size', source=g, color = 'colors', alpha = 0.8,legend_field='continent_no') #formatting: p.xaxis.major_label_overrides = { 1000: '1k', 10000: '10k', 100000: '100k' } p.legend.location = "top_left" p.legend[0].border_line_alpha=0 p.outline_line_alpha=0 p.grid[0].grid_line_alpha=0 p.grid[1].grid_line_alpha=0 #defining the outputfile: output_file("./BNP_levealder.html") #drawing it: show(p)