Bokeh is a newly introduced Python library, like D3.js, which is used for interactive data visualization targeting web browsers. Bokeh distinguishes itself from other Python visualization libraries such as Matplotlib or Seaborn by providing precise and elegant construction of versatile graphics with high interactivity and high performance in large and streaming data sets. Bokeh can also be used to embed visualizations to Django and Flask.
In this cheat sheet, we will learn the basics of creating plots with the help of Bokeh's high-level module, bokeh.plotting
.
Below are the steps for creating a plot. We will discuss each step in detail later.
Importing the library and its API
Preparing the data
Specifying how and where the file will be saved
Creating a new plot and adding renderers for your data with visual customizations
Here is a simple example in Bokeh:
1# Step1 = importing the library
2from bokeh.plotting import figure, output_file, show
3
4# Step2= preparing the data
5x = [1,5,3,4]
6y = [1,2,3,5]
7
8# Step3 = Specify name and location
9output_file('index.html')
10
11# Step4 = create a new
12p = figure(plot_height=250, plot_width=300)
13
14# adding a legend and line renderer with width
15p.line(x,y, color = 'Red')
16
17# Step 5 = show result
18show(p)
We are working on how to plot using bokeh.plotting
, a high-level module of the bokeh library. bokeh.plotting
is mainly centered around the figure()
class, so we will look at how to import this module.
1from bokeh.plotting import figure
Importing the API, if we want to use Columndatasource
:
1from bokeh.models import ColumnDataSource
Importing of the bokeh.io
, if we want to specify how and where our file will be saved:
1from bokeh.io import output_notebook, show
We can also import figure
and output_notebook
simultaneously
.
1from bokeh.plotting import figure, output_file, show
For gridplot
, we have to import gridplot
function from bokeh.layouts
.
1from bokeh.layouts import gridplot
1from bokeh.plotting import figure
2x_values = [4, 2, 7, 4, 8]
3y_values = [4, 5, 2, 6, 9]
4
5p = figure()
6p.circle(x=x_values, y=y_values)
The data we pass directly or by Pandas is converted to columndatasource
, but we can also pass it directly.
1from bokeh.plotting import figure
2from bokeh.models import ColumnDataSource
3
4# passing data in columndatasource
5data = {'x_values': [4, 2, 7, 4, 8],
6 'y_values': [2, 6, 9, 3, 6]}
7source = ColumnDataSource(data=data)
8
9p = figure()
10p.circle(x = 'x_values', y = 'y_values', source = source)
We can use bokeh.io
API to name or to save our output file. It is advised to specify or name the file before adding the plot and render it to the plot.
output_file()
is called for naming the the file or to generate default output state when show()
and save()
functions are called
.
1# importing the output_file() function
2from bokeh.io import output_file
3output_file("name.html")
export_png()
is used to export the file as .png file type. This function usage is similar to save()
and show
functions
.
1# importing the export_png() function
2from bokeh.io import export_png
3
4# exporting the resulted file as png
5export_png(plot, filename = "plot.png")
export_svgs()
is used to export the file as .svg file type. This function usage is also similar to save()
and show
functions
.
1# importing the export_svgs() function
2from bokeh.io import export_svgs
3
4plot.output_backend = "svg"
5# exporting the resulted file as svgs
6export_svgs(plot, filename = "plot.svg")
By passing the push_notebook()
function, we can make Bokeh show the results of show()
calls on the Jupyter output cell since the last call of push_notebook()
.
1from bokeh.plotting import figure
2from bokeh.io import output_notebook, push_notebook, show
3
4output_notebook()
5
6plot = figure(plot_height= 250, plot_width = 300)
7plot.circle([1,2,3], [4,6,5], size = 10, color ='red')
8
9handle = show(plot, notebook_handle=True)
10
11# Update the plot title in the earlier cell
12plot.title.text = "Test Title"
13push_notebook(handle = handle)
In this screenshot, you can see the plot shown in the output cell of the Jupyter Notebook.
First, we have to create a new figure for plotting using figure()
class. figure()
objects have many glyph methods that can be used to draw different types of plots
:
1from bokeh.plotting import figure, output_file, show
2
3#plot creation and adding renders
4p = figure(plot_width = 250, plot_height = 300)
5p.line([1,2,3], [2,3,4], line_width = 4)
6show(p)
1from bokeh.plotting import figure, output_file, show
2x = [1,2,3,4]
3y = [4,6,7,8]
4x1 = [5,7,8,9]
5y1 = [7,8,3,2]
6# crearing a plot
7p = figure()
8# adding renders to the plot
9p.multi_line([x, y], [x1, y1], color=["Red", "navy"], alpha=[0.8, 0.3], line_width=4)
10show(p)
1from bokeh.plotting import figure, output_file, show
2
3# plot creation and adding renders
4plot = figure(plot_width=300, plot_height=300)
5plot.circle(x=[1, 2, 3], y=[1, 2, 3], size=20)
6
7show(plot)
1import numpy as np
2from bokeh.plotting import figure, output_file, show
3# importing gridplot function
4from bokeh.layouts import gridplot
5
6# prepare some data using numpy
7x = np.linspace(0, 15, 80)
8y = np.cos(x)
9y1 = np.sin(x)
10y2 = np.sin(x) + np.cos(x)
11
12# outputfile
13output_file("grid.html")
14
15# Create a new plot
16a1 = figure(width=250, plot_height=250, title=None)
17a1.circle(x, y, size=10, color="yellow", alpha=0.5)
18
19# New plot with same range
20a2 = figure(width=250, height=250, x_range=a1.x_range, y_range=a1.y_range, title=None)
21a2.triangle(x, y1, size=10, color="Grey", alpha=0.5)
22
23# New plot with x axis same range
24a3 = figure(width=250, height=250, x_range=a1.x_range, title=None)
25a3.square(x, y2, size=10, color="green", alpha=0.5)
26
27# NEW: put the subplots in a gridplot
28p = gridplot([[a1, a2, a3]], toolbar_location=None)
29
30#show result
31show(p)
1from bokeh.plotting import figure, output_file, show
2# Adjusting the plot width and height
3plot = figure(plot_width=250, plot_height=300)
4# Rendering the glyph
5plot.wedge(x = [4, 5, 6], y = [2, 3, 4],
6 radius = 17, start_angle=0.8, end_angle=3.1,
7 radius_units="screen", color="green")
8show(plot)
There is also a scatter
function that can be parameterized by marker type
.
1# Import Bokeh Libraries
2from bokeh.plotting import figure, output_file, show
3
4# x-y coordinate data
5x = [1.5, 2.5, 1]
6y = [1, 1.8, 2.5]
7#Specify fie name
8output_file('scatter.html', title='Circle Glyphs')
9#plot creation
10p = figure(
11 title='simple scatter plot',
12 plot_height=250, plot_width=300
13)
14#using scatter function
15p.scatter(x,y, marker="square",size = 16, fill_color="red")
16show(p)
We can paramaterize different types of markers with the scatter
function.
Note: Every type of marker has a different number of parameters and keywords according to its properties:
There are also specialized glyphs for making bar charts in Bokeh:
1from bokeh.plotting import figure, output_file, show
2
3plot = figure(plot_width=250, plot_height=300)
4# rendering the hbar glyph
5plot.hbar(y = [1, 3, 6], height = 0.6, left = 0.1, right = [1,2,3], color = "red")
6show(plot)
Other types of stack bars include:
Bars: hbar_stack(), vbar_stack()
Lines: hline_stack(), vline_stack()
This is the last step of creating a Bokeh plot after rendering and creating a plot.
To display the file, show( )
function is used. We can call this multiple times in a single cell of Jupyter Notebook to display multiple objects.
1#importing the show() function
2from bokeh.io import output_file, show
3#naming the output file
4output_file("test.html")
5# show results
6show(obj)
to save the file, save( )
function is used. We can call this multiple times in a single Jupyter Notebook to save multiple objects.
1# importing the save() function
2from bokeh.io import output_file, save
3# naming the output file
4output_file("test.html")
5# save result
6save(obj)
In this guide, we have gone through the basics to create a plot using Bokeh's high-level module bokeh.plotting
. This cheat sheet aims to remind you of syntax rules, but also of important concepts as well. I hope itwill be really helpful when you’re trying a set of exercises related to a specific topic or working on a project. For further queries, contact me at CodeAlphabet.