title: Bokeh-Visualisation


Bokeh is a JavaScript based data visualisation library that specialises in interactive plots.

Bokeh is a Python library for creating interactive visualizations for modern web browsers. It helps you build beautiful graphics, ranging from simple plots to complex dashboards with streaming datasets. With Bokeh, you can create JavaScript-powered visualizations without writing any JavaScript yourself.

The documentation includes a user-guide, a gallery of examples, a reference guide and a tutorial.

  • Bokeh visualisations are created with plot elements called glyphs (such as bars, lines circles etc) that can be individually customised.
  • The glyphs are placed on a canvas or figure.
  • Bokeh visualisations can be easily formatted and exported to be embedded in a web page.
  • The figure() function is used to create a figure object to which the various elements are added.
  • Bokeh's show() function is used to show the plot.
  • output_file() function to define the name of the file to create. Usually the name of the output file is placed at the top of the python file after importing the modules.

install using pip install bokeh.

Import the library

In [1]:
from bokeh.plotting import figure, show
from bokeh.io import output_file
output_file("my-first-bokeh.html")

x = [1,3,5,6,8,9] y = [2,4,6,8,9,13] z = list(range(3,9))

The plot figure can be customised by providing arguments to the figure object such as background_fill_color, background_fill_alpha, plot_height, plot_width, x_axis_label, y_axis_label.

p = figure(background_fill_color='gray', background_fill_alpha=0.3,plot_height=500, plot_width=600) p.circle(x,y, size=10, color="red",legend_label='circle') p.triangle(x,z,size=10, color="green",legend_label='triangle') p.line(x,y, color="blue") p.legend.click_policy='hide' show(p)

  • Some elements can be set after the plot has been displayed. For example grid lines can be removed by selecting the figure, the grid element then change that feature:
    fig.grid.grid_line_color= None
    show(p)
    
  • Can add titles to a figure using add_layout() and Title functions which allows you greater control over where the title is placed. Title has to be first imported from bokeh.models.
from boken.models import Title
p.add_layout(Title(text="Add a title", align='center'),"below")
show(p)

from bokeh.models import Title p.add_layout(Title(text="Adding a title", align='center'),"below") show(p)

Working with pandas DataFrames

Bokeh has a wrapper object for Pandas DataFrames called ColumnDataSource that makes them easier to work with and allows data to be shared between multiple plots. First need to import the ColumnDataSource from boken.models.

ColumnDataSource can also work with data objects from other libraries such as Seaborn.

In [3]:
import pandas as pd
from bokeh.models import ColumnDataSource
from bokeh.models import Title
In [4]:
df = pd.read_csv("datasets/gapminder.csv")
In [5]:
df.info()
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 10111 entries, 0 to 10110
Data columns (total 8 columns):
 #   Column           Non-Null Count  Dtype  
---  ------           --------------  -----  
 0   Country          10111 non-null  object 
 1   Year             10111 non-null  int64  
 2   fertility        10100 non-null  float64
 3   life             10111 non-null  float64
 4   population       10108 non-null  float64
 5   child_mortality  9210 non-null   float64
 6   gdp              9000 non-null   float64
 7   region           10111 non-null  object 
dtypes: float64(5), int64(1), object(2)
memory usage: 632.1+ KB
In [6]:
df.head()
Out[6]:
Country Year fertility life population child_mortality gdp region
0 Afghanistan 1964 7.671 33.639 10474903.0 339.7 1182.0 South Asia
1 Afghanistan 1965 7.671 34.152 10697983.0 334.1 1182.0 South Asia
2 Afghanistan 1966 7.671 34.662 10927724.0 328.7 1168.0 South Asia
3 Afghanistan 1967 7.671 35.170 11163656.0 323.3 1173.0 South Asia
4 Afghanistan 1968 7.671 35.674 11411022.0 318.1 1187.0 South Asia
In [7]:
df.Country.unique()
Out[7]:
array(['Afghanistan', 'Albania', 'Algeria', 'Angola',
       'Antigua and Barbuda', 'Argentina', 'Armenia', 'Aruba',
       'Australia', 'Austria', 'Azerbaijan', 'Bahamas', 'Bahrain',
       'Bangladesh', 'Barbados', 'Belarus', 'Belgium', 'Belize', 'Benin',
       'Bhutan', 'Bolivia', 'Bosnia and Herzegovina', 'Botswana',
       'Brazil', 'Brunei', 'Bulgaria', 'Burkina Faso', 'Burundi',
       'Cambodia', 'Cameroon', 'Canada', 'Cape Verde',
       'Central African Rep.', 'Chad', 'Channel Islands', 'Chile',
       'China', 'Colombia', 'Comoros', 'Congo, Dem. Rep.', 'Congo, Rep.',
       'Costa Rica', "Cote d'Ivoire", 'Croatia', 'Cuba', 'Cyprus',
       'Czech Rep.', 'Denmark', 'Djibouti', 'Dominican Rep.', 'Ecuador',
       'Egypt', 'El Salvador', 'Equatorial Guinea', 'Eritrea', 'Estonia',
       'Ethiopia', 'Fiji', 'Finland', 'France', 'French Guiana',
       'French Polynesia', 'Gabon', 'Gambia', 'Georgia', 'Germany',
       'Ghana', 'Greece', 'Greenland', 'Grenada', 'Guadeloupe', 'Guam',
       'Guatemala', 'Guinea', 'Guinea-Bissau', 'Guyana', 'Haiti',
       'Honduras', 'Hong Kong, China', 'Hungary', 'Iceland', 'India',
       'Indonesia', 'Iran', 'Iraq', 'Ireland', 'Israel', 'Italy',
       'Jamaica', 'Japan', 'Jordan', 'Kazakhstan', 'Kenya', 'Kiribati',
       'Korea, Dem. Rep.', 'Korea, Rep.', 'Kuwait', 'Kyrgyzstan', 'Laos',
       'Latvia', 'Lebanon', 'Lesotho', 'Liberia', 'Libya', 'Lithuania',
       'Luxembourg', 'Macao, China', 'Macedonia, FYR', 'Madagascar',
       'Malawi', 'Malaysia', 'Maldives', 'Mali', 'Malta', 'Martinique',
       'Mauritania', 'Mauritius', 'Mayotte', 'Mexico',
       'Micronesia, Fed. Sts.', 'Moldova', 'Mongolia', 'Montenegro',
       'Morocco', 'Mozambique', 'Myanmar', 'Namibia', 'Nepal',
       'Netherlands', 'Netherlands Antilles', 'New Caledonia',
       'New Zealand', 'Nicaragua', 'Niger', 'Nigeria', 'Norway', 'Oman',
       'Pakistan', 'Panama', 'Papua New Guinea', 'Paraguay', 'Peru',
       'Philippines', 'Poland', 'Portugal', 'Puerto Rico', 'Qatar',
       'Reunion', 'Romania', 'Russia', 'Rwanda', 'Saint Lucia',
       'Saint Vincent and the Grenadines', 'Samoa',
       'Sao Tome and Principe', 'Saudi Arabia', 'Senegal', 'Serbia',
       'Seychelles', 'Sierra Leone', 'Singapore', 'Slovak Republic',
       'Slovenia', 'Solomon Islands', 'Somalia', 'South Africa',
       'South Sudan', 'Spain', 'Sri Lanka', 'Sudan', 'Suriname',
       'Swaziland', 'Sweden', 'Switzerland', 'Syria', 'Taiwan',
       'Tajikistan', 'Tanzania', 'Thailand', 'Timor-Leste', 'Togo',
       'Tokelau', 'Tonga', 'Trinidad and Tobago', 'Tunisia', 'Turkey',
       'Turkmenistan', 'Uganda', 'Ukraine', 'United Arab Emirates',
       'United Kingdom', 'United States', 'Uruguay', 'Uzbekistan',
       'Vanuatu', 'Venezuela', 'Vietnam', 'Virgin Islands (U.S.)',
       'West Bank and Gaza', 'Western Sahara', 'Yemen, Rep.', 'Zambia',
       'Zimbabwe', 'Ă…land'], dtype=object)
In [8]:
ireland = df[df.Country=="Ireland"]
afghanistan = df[df.Country=="Afghanistan"]
portugal = df[df.Country=="Portugal"]
usa = df[df.Country=="United States"]
spain = df[df.Country=="Spain"]
In [9]:
source1 = ColumnDataSource(ireland)
source2= ColumnDataSource(afghanistan)
source3=ColumnDataSource(portugal)
source4 = ColumnDataSource(usa)
source5 = ColumnDataSource(spain)

plot = figure() plot.circle(x="Year", y = "life", source = source1) plot.add_layout(Title(text="Life expectancy in Ireland", align='center'),"below") show(plot)

In [10]:
output_file("another-bokeh.html")

Multiple Visualisations

You can create multiple visualisations for the same figure. Create a figure for each plot, then set them up to display in a vertical or horizontal orientation using the column() and row() functions which are imported from bokeh.layouts.

In [11]:
from bokeh.layouts import row, column
In [12]:
plot1 = figure()
plot1.circle(x="Year", y = "life", source = source1, color="green")
plot1.add_layout(Title(text="Ireland", align='center'),"below")
plot2 = figure()
plot2.circle(x="Year", y = "life", source = source2, color="red")
plot2.add_layout(Title(text="Afghanistan", align='center'),"below")
plot3 = figure()
plot3.circle(x="Year", y = "life", source = source3, color="orange")
plot3.add_layout(Title(text="Portugal", align='center'),"below")
plot4 = figure()
plot4.circle(x="Year", y = "life", source = source3, color="blue")
plot4.add_layout(Title(text="USA", align='center'),"below")
In [ ]:
 
In [13]:
figures1 = row(plot1, plot2)
figures2 = row(plot3, plot4)
layout1 = column(figures1, figures2)
show(layout1)

fig.grid.grid_line_color= None show(p)

Making plots interactive

Default Bokeh figures have their own toolbar that allow you to zoom in and out, save, reload etc. You can change the position of the toolbar or even remove it.

Selection Tools

You can enable the user to select some data points by adding some keywords when setting up the glyphs in the figure . Tools include "box select", "lasso select", "poly select" to select certain data points.

ireland.head()

select_tools2 = ['lasso_select', 'box_select','poly_select', 'tap','reset','save'] myplot2 = figure(tools= select_tools2) myplot2.circle(x = "life",y = "child_mortality", source = source5, color = "green", selection_color="orange", nonselection_fill_alpha = 0.4, nonselection_fill_color="black") show(myplot2)

In [ ]:
 
In [ ]: