Thanks to

Basics of Bokeh


Basics of Bokeh


Bokeh provides a quick and simple interface to data scientists and domain experts who do not want to be distracted by the details of the software. Bokeh is actually composed of two library components for the interface.

  • bokeh.models:
    A low-level interface that provides the most flexibility to application developers
  • bokeh.plotting:
    A higher-level interface centered around composing visual glyphs

More about these interfaces can be understood from the following examples in this tutorial

Simple quickstart example with explanation:

### Simple quickstart example with explanation

#Import necessary modules
from bokeh.plotting import figure, output_file, show

#Prepare some data
x = np.random.rand(4)
y = np.random.rand(4)

#Define output

#Create a new plot with a title and axis labels
p = figure(title="simple line example", x_axis_label='x', y_axis_label='y')

#Add a line renderer with legend and line thickness
p.line(x, y, legend_label="Temp.", line_width=2)

#Show the results


bokeh plot

Explanation of Quickstart example:

  • Import necessary modules: In the above example, we imported necessary modules( figure, output_file, show)from bokeh.plotting library. There are a lot of other modules that are usually imported as per the necessity of the plot
  • Prepare some data: In this case, we created NumPy array from the random number as data to plot. Data can be plain python list, NumPy arrays or Pandas DataDrames or Series
  • Define your output: In this case using output_file(), with the filename “lines.html”. Another option is output_notebook() for use in Jupyter notebooks
  • Call figure(): This creates a plot with typical default options and easy customization of title, tools, and axes labels. Note that we can extend this function to customize the output plot
  • Add renderers: In this case, we use line() for our data, specifying visual customizations like colors, legends, and widths
  • show() or save() the results: These functions save the plot to an HTML file and optionally display it in a browser

Creating layouts

Bokeh includes several layout options for arranging plots and widgets. The layout functions let you build a grid of plots and widgets. You can nest as many rows, columns, or grids of plots together as you’d like. A simple example of creating layouts is illustrated below:

#import necessary modules
from import output_file, show
from bokeh.layouts import column,row
from bokeh.plotting import figure

#Data preparation
x = list(range(11))
y0 = x
y1 = [10 - i for i in x]
y2 = [abs(i - 5) for i in x]

#Define output

#Create two plots
s1 = figure(plot_width=250, plot_height=250, background_fill_color="#fafafa"), y0, size=12, color="#53777a", alpha=0.8)
s2 = figure(plot_width=250, plot_height=250, background_fill_color="#fafafa")
s2.triangle(x, y1, size=12, color="#c02942", alpha=0.8)

#Put the results in a column ,rows and show
show(column(s1, s2)) #creates cloumn layout
show(row(s1,s2))     #creates row layout


Basics of Bokeh
Basics of Bokeh

Adding Annotations

Bokeh includes several different types of annotations to allow users to add supplemental information to their visualizations like Titles, Legends, Explicit Index, Interactive Legends, Color Bars, Arrows, Bands, Labels and many more. The example below illustrates the addition of important annotations:

#import necessary modules
from bokeh.plotting import figure, show, output_file
from bokeh.models import ColumnDataSource, Range1d, LabelSet, Label

#Prepare data

#Define output

#Define figure()
p = figure(plot_width=300, plot_height=300)[1,2], [3,4])

#Configure visual properties 
p.title.text = "Title"
p.title.align = "center"
p.title.text_color = "orange"
p.title.text_font_size = "25px"
p.yaxis.axis_label = 'y'
p.xaxis.axis_label = 'x'

#Show the plot


Basics of Bokeh

Configuring Plot Tools

Bokeh contains different interactive tools that can be used to report information, to change plot parameters such as zoom level or range extents, or to add, edit, or delete glyphs that can be noticed from a side of plots. The example below shows how we can configure these plot tools.

#Importing necessary modules
from bokeh.plotting import figure, output_file, show
from bokeh.models import BoxSelectTool

#Define output

#Create a new plot with the toolbar below 
p = figure(tools=toolset
           ,plot_width=400, plot_height=400,
          title=None, toolbar_location="below")

#Add tools

#Configure so that Bokeh chooses what (if any) scroll tool is active
p.toolbar.active_scroll = "auto"

#Plot the circles[1, 2, 3, 4, 5], [2, 5, 8, 2, 7], size=10)

#Show the output


Basics of Bokeh

Data Sources and transformations

The ColumnDataSource is a mapping of column names (strings) to sequences of values. The mapping is provided by passing a Python dict with string keys and simple Python lists as values. The values could also be NumPy arrays or Pandas sequences. Once the ColumnDataSource has been created, it can be passed into the source parameter of plotting methods which allows you to pass a column’s name as a stand-in for the data values. It should be noticed that the columns in a ColumnDataSource must always be the SAME length. NOTE: This section deals with using pandas DataFrame.If you are unfamiliar with this quick tutorial will help

# A simple example of ColumnDataSource and HoverTool usage

#Importing necessary modules
import pandas as pd
from bokeh.models import ColumnDataSource
from import HoverTool

#Getting data as pandas dataframe

#Create a ColumnDataSource
s2= ColumnDataSource(df)

#Define output

#Ploting the figure
p = figure(plot_width=680, plot_height=400)
p.xaxis.axis_label = 'Year'
p.yaxis.axis_label = 'Divorces''Year',y='Divorces',size=10,source=s2,color='green')#or we can directly pass dataframe to this func to it converts automatically

#Manage Hovertool() function in plot
hover = HoverTool()
    ('Total Marriages', '@Marriages'),
    ('Total Population', '@Population'),
    ('Marriages per 1000', '@Marriages_per_1000'),
    ('Divorces per 1000', '@Divorces_per_1000')

#Add hover tool to figure

#Show the figure


   Year  Marriages  Divorces  Population  Marriages_per_1000  \
0  1867   357000.0   10000.0    36970000                 9.7   
1  1868   345000.0   10000.0    37885000                 9.1   
2  1869   348000.0   11000.0    38870000                 9.0   
3  1870   352000.0   11000.0    39905000                 8.8   
4  1871   359000.0   12000.0    41010000                 8.8   

0                0.3  
1                0.3  
2                0.3  
3                0.3  
4                0.3  
Basics of Bokeh

Learn about plotting in Bokeh in the next lesson of the course.

Leave your thought here

Your email address will not be published. Required fields are marked *