title: Bokeh date: 2021/03/05 author: Angela categories:
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.
figure()
function is used to create a figure object to which the various elements are added.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
.
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)
fig.grid.grid_line_color= None
show(p)
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)
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.
import pandas as pd
from bokeh.models import ColumnDataSource
from bokeh.models import Title
df = pd.read_csv("datasets/gapminder.csv")
df.info()
df.head()
df.Country.unique()
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"]
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)
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
.
from bokeh.layouts import row, column
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")
figures1 = row(plot1, plot2)
figures2 = row(plot3, plot4)
layout1 = column(figures1, figures2)
show(layout1)
fig.grid.grid_line_color= None show(p)
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.
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)