Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • Bokeh
  • Matplotlib
  • Pandas
  • Seaborn
  • Ggplot
  • Plotly
  • Altair
  • Networkx
  • Machine Learning Math
  • Machin Learning
  • Data Analysis
  • Deep Learning
  • Deep Learning Projects
  • NLP
  • Computer vision
  • Data science
  • Machin Learning Interview question
  • Deep learning interview question
Open In App
Next Article:
Introduction to Bokeh in Python
Next article icon

Python Bokeh tutorial – Interactive Data Visualization with Bokeh

Last Updated : 15 Mar, 2023
Comments
Improve
Suggest changes
Like Article
Like
Report

Python Bokeh is a Data Visualization library that provides interactive charts and plots. Bokeh renders its plots using HTML and JavaScript that uses modern web browsers for presenting elegant, concise construction of novel graphics with high-level interactivity. 

Features of Bokeh:

  • Flexibility: Bokeh can be used for common plotting requirements and for custom and complex use-cases.
  • Productivity: Its interaction with other popular Pydata tools (such as Pandas and Jupyter notebook) is very easy.
  • Interactivity: It creates interactive plots that change with the user interaction.
  • Powerful: Generation of visualizations for specialized use-cases can be done by adding JavaScript.
  • Shareable: Visual data are shareable. They can also be rendered in Jupyter notebooks.
  • Open source: Bokeh is an open-source project.
Python Bokeh Tutorial

This tutorial aims at providing insight to Bokeh using well-explained concepts and examples with the help of a huge dataset. So let’s dive deep into the Bokeh and learn all it from basic to advance.

Table Of Content
 

  • Installation
  • Bokeh Interfaces – Basic Concepts of Bokeh
  • Getting Started
  • Annotations and Legends 
    • Customizing Legends 
       
  • Plotting Different Types of Plots 
    • Bar Plot
    • Scatter Plot
    • Patch Plot
    • Area Plot
    • Pie Chart
  • Creating Different Shapes 
    • Circle
    • Oval
    • Triangle
    • Rectangle
    • Polygon
  • Plotting Multiple Plots 
    • Vertical Layouts
    • Horizontal Layout
    • Grid Layout
  • Interactive Data Visualization 
    • Configuring Plot Tools
    • Interactive Legends
    • Adding Widgets to the Plot
  • Creating Different Types of Glyphs
  • Visualizing Different Types of Data
  • More Topics on Bokeh

Installation

Bokeh is supported by CPython 3.6 and older with both standard distribution and anaconda distribution. Bokeh package has the following dependencies.

1. Required Dependencies

  • PyYAML>=3.10
  • python-dateutil>=2.1
  • Jinja2>=2.7
  • numpy>=1.11.3
  • pillow>=4.0
  • packaging>=16.8
  • tornado>=5
  • typing_extensions >=3.7.4

2. Optional Dependencies

  • Jupyter
  • NodeJS
  • NetworkX
  • Pandas
  • psutil
  • Selenium, GeckoDriver, Firefox
  • Sphinx

Bokeh can be installed using both conda package manager and pip. To install it using conda type the below command in the terminal.

conda install bokeh

This will install all the dependencies. If all the dependencies are installed then you can install the bokeh from PyPI using pip. Type the below command in the terminal.

pip install bokeh

Refer to the below article to get detailed information about the installation of Bokeh.

  • Python – Setting up the Bokeh Environment

Bokeh Interfaces – Basic Concepts of Bokeh

Bokeh is simple to use as it provides a simple interface to the data scientists who do not want to be distracted by its implementation and also provides a detailed interface to developers and software engineers who may want more control over the Bokeh to create more sophisticated features. To do this Bokeh follows the layered approach. 

Bokeh.models

This class is the Python Library for Bokeh that contains model classes that handle the JSON data created by Bokeh’s JavaScript library (BokehJS). Most of the models are very basic consisting of very few attributes or no methods.

bokeh.plotting

This is the mid-level interface that provides Matplotlib or MATLAB like features for plotting. It deals with the data that is to be plotted and creating the valid axes, grids, and tools. The main class of this interface is the Figure class.

Getting Started

After the installation and learning about the basic concepts of Bokeh let’s create a simple plot.

Example:

Python3

# importing the modules 
from bokeh.plotting import figure, output_file, show 
  
# instantiating the figure object 
graph = figure(title = "Bokeh Line Graph") 
  
# the points to be plotted 
x = [1, 2, 3, 4, 5] 
y = [5, 4, 3, 2, 1] 
  
# plotting the line graph 
graph.line(x, y) 
  
# displaying the model 
show(graph)
                      
                       

Output:

Bokeh Tutorial simple plot

In the above example, we have created a simple Plot with the Title as Bokeh Line Graph. If you are using Jupyter then the output will be created in a new tab in the browser.

Annotations and Legends

Annotations are the supplemental information such as titles, legends, arrows, etc that can be added to the graphs. In the above example, we have already seen how to add the titles to the graph. In this section, we will see about the legends.

Adding legends to your figures can help to properly describe and define them. Hence, giving more clarity. Legends in Bokeh are simple to implement. They can be basic, automatically grouped, manually mentioned, explicitly indexed, and also interactive.

Example:

Python3

# importing the modules
from bokeh.plotting import figure, output_file, show
  
# instantiating the figure object
graph = figure(title="Bokeh Line Graph")
  
# the points to be plotted
x = [1, 2, 3, 4, 5]
y = [5, 4, 3, 2, 1]
  
# plotting the 1st line graph
graph.line(x, x, legend_label="Line 1")
  
# plotting the 2nd line graph with a
# different color
graph.line(y, x, legend_label="Line 2",
           line_color="green")
  
# displaying the model
show(graph)
                      
                       

Output:

Bokeh tutorial annotations and legends

In the above example, we have plotted two different lines with a legend that simply states that which is line 1 and which is line 2. The color in the legends is also differentiated by the color.

Refer to the below articles to get detailed information about the annotations and legends

  • Bokeh – Annotations and Legends

Customizing Legends

Legends in Bokeh can be customized using the following properties.

PropertyDescription
legend.label_text_font change default label font to specified font name
legend.label_text_font_size font size in points
legend.location set the label at specified location.
legend.title set title for legend label 
legend.orientation set to horizontal (default) or vertical
legend.clicking_policy specify what should happen when legend is clicked

Example:

Python3

# importing the modules
from bokeh.plotting import figure, output_file, show
  
# instantiating the figure object
graph = figure(title="Bokeh Line Graph")
  
# the points to be plotted
x = [1, 2, 3, 4, 5]
y = [5, 4, 3, 2, 1]
  
# plotting the 1st line graph
graph.line(x, x, legend_label="Line 1")
  
# plotting the 2nd line graph with a
# different color
graph.line(y, x, legend_label="Line 2",
           line_color="green")
  
graph.legend.title = "Title of the legend"
graph.legend.location ="top_left"
graph.legend.label_text_font_size = "17pt"
  
# displaying the model
show(graph)
                      
                       

Output:

bokeh tutorial customize legend

Plotting Different Types of Plots

Glyphs in Bokeh terminology means the basic building blocks of the Bokeh plots such as lines, rectangles, squares, etc. Bokeh plots are created using the bokeh.plotting interface which uses a default set of tools and styles.

Line Plot

Line charts are used to represent the relation between two data X and Y on a different axis. A line plot can be created using the line() method of the plotting module.

Syntax:

line(parameters)

Example:

Python3

# importing the modules 
from bokeh.plotting import figure, output_file, show 
  
# instantiating the figure object 
graph = figure(title = "Bokeh Line Graph") 
  
# the points to be plotted 
x = [1, 2, 3, 4, 5] 
y = [5, 4, 3, 2, 1] 
  
# plotting the line graph 
graph.line(x, y) 
  
# displaying the model 
show(graph)
                      
                       

Output:

bokeh tutorial line plot

Refer to the below articles to get detailed information about the line plots.

  • Python Bokeh – Plotting a Line Graph
  • Python Bokeh – Plotting Multiple Lines on a Graph

Bar Plot

Bar plot or Bar chart is a graph that represents the category of data with rectangular bars with lengths and heights that is proportional to the values which they represent. It can be of two types horizontal bars and vertical bars. Each can be created using the hbar() and vbar() functions of the plotting interface respectively.

Syntax:

hbar(parameters)

vbar(parameters)

Example 1: Creating horizontal bars.

Python3

# importing the modules 
from bokeh.plotting import figure, output_file, show 
  
# instantiating the figure object 
graph = figure(title = "Bokeh Bar Graph") 
  
# the points to be plotted 
x = [1, 2, 3, 4, 5] 
y = [1, 2, 3, 4, 5]  
  
# height / thickness of the plot
height = 0.5
  
# plotting the bar graph 
graph.hbar(x, right = y, height = height) 
  
# displaying the model 
show(graph)
                      
                       

Output:

bokeh tutorial hbar

Example 2: Creating the vertical bars

Python3

# importing the modules 
from bokeh.plotting import figure, output_file, show 
  
# instantiating the figure object 
graph = figure(title = "Bokeh Bar Graph") 
  
# the points to be plotted 
x = [1, 2, 3, 4, 5] 
y = [1, 2, 3, 4, 5]  
  
# height / thickness of the plot
width = 0.5
  
# plotting the bar graph 
graph.vbar(x, top = y, width = width) 
  
# displaying the model 
show(graph)
                      
                       

Output:

bokeh tutorial vbar

Refer to the below articles to get detailed information about the bar charts.

  • Python Bokeh – Plotting Horizontal Bar Graphs
  • Python Bokeh – Plotting Vertical Bar Graphs

Scatter Plot

A scatter plot is a set of dotted points to represent individual pieces of data in the horizontal and vertical axis. A graph in which the values of two variables are plotted along X-axis and Y-axis, the pattern of the resulting points reveals a correlation between them. It can be plotted using the scatter() method of the plotting module.

Syntax:

scatter(parameters)

Example:

Python3

# importing the modules 
from bokeh.plotting import figure, output_file, show 
from bokeh.palettes import magma 
import random 
  
      
# instantiating the figure object 
graph = figure(title = "Bokeh Scatter Graph") 
  
# points to be plotted 
x = [n for n in range(256)] 
y = [random.random() + 1 for n in range(256)] 
  
  
# plotting the graph 
graph.scatter(x, y) 
  
# displaying the model 
show(graph)
                      
                       

Output:

bokeh tutorial scatter plot

Refer to the below articles to get detailed information about the scatter plots.

  • Python Bokeh – Plotting a Scatter Plot on a Graph

Patch Plot

Patch Plot shades a region of area to show a group having same properties. It can be created using the patch() method of the plotting module.

Syntax:

patch(parameters)

Example:

Python3

# importing the modules 
from bokeh.plotting import figure, output_file, show 
from bokeh.palettes import magma 
import random 
  
      
# instantiating the figure object 
graph = figure(title = "Bokeh Patch Plo") 
  
# points to be plotted 
x = [n for n in range(256)] 
y = [random.random() + 1 for n in range(256)] 
  
# plotting the graph 
graph.patch(x, y) 
  
# displaying the model 
show(graph)
                      
                       

Output:

patch plot bokeh tutorial

Refer to the below articles to get detailed information about the Patch Plot.

  • Python Bokeh – Plotting Patches on a Graph

Area Plot

Area plots are defined as the filled regions between two series that share a common areas. Bokeh Figure class has two methods which are – varea(), harea()

Syntax:

varea(x, y1, y2, **kwargs)

harea(x1, x2, y, **kwargs)

Example 1: Creating vertical area plot

Python

# Implementation of bokeh function
import numpy as np 
from bokeh.plotting import figure, output_file, show 
      
x = [1, 2, 3, 4, 5] 
y1 = [2, 4, 5, 2, 4] 
y2 = [1, 2, 2, 3, 6] 
  
p = figure(plot_width=300, plot_height=300) 
  
# area plot 
p.varea(x=x, y1=y1, y2=y2,fill_color="green") 
  
show(p)
                      
                       

Output:

Example 2: Creating horizontal area plot

Python3

# Implementation of bokeh function 
      
import numpy as np 
from bokeh.plotting import figure, output_file, show 
      
y = [1, 2, 3, 4, 5] 
x1 = [2, 4, 5, 2, 4] 
x2 = [1, 2, 2, 3, 6] 
  
p = figure(plot_width=300, plot_height=300) 
  
# area plot 
p.harea(x1=x1, x2=x2, y=y,fill_color="green") 
  
show(p)
                      
                       

Output:

Refer to the below articles to get detailed information about the area charts

  • Make an area plot in Python using Bokeh

Pie Chart

Bokeh Does not provide a direct method to plot the Pie Chart. It can be created using the wedge() method. In the wedge() function, the primary parameters are the x and y coordinates of the wedge, the radius, the start_angle and the end_angle of the wedge. In order to plot the wedges in such a way that they look like a pie chart, the x, y, and radius parameters of all the wedges will be the same. We will only adjust the start_angle and the end_angle.

Syntax:

wedge(parameters)

Example: 

Python3

# importing the modules 
from bokeh.plotting import figure, output_file, show 
          
# instantiating the figure object 
graph = figure(title = "Bokeh Wedge Graph") 
      
# the points to be plotted 
x = 0
y = 0
  
# radius of the wedge 
radius = 15
  
# start angle of the wedge 
start_angle = 1
  
# end angle of the wedge 
end_angle = 2
  
# plotting the graph 
graph.wedge(x, y, radius = radius, 
            start_angle = start_angle, 
            end_angle = end_angle) 
      
# displaying the model 
show(graph) 
                      
                       

Output:

Bokeh Tutorial Pie chart

Refer to the below articles to get detailed information about the pie charts.

  • Python Bokeh – Plotting Wedges on a Graph
  • Python Bokeh – Making a Pie Chart

Creating Different Shapes

The Figure class in Bokeh allows us create vectorised glyphs of different shapes such as circle, rectangle, oval, polygon, etc. Let’s discuss them in detail.

Circle

Bokeh Figure class following methods to draw circle glyphs which are given below:

  • circle() method is a used to add a circle glyph to the figure and needs x and y coordinates of its center.
  • circle_cross() method is a used to add a circle glyph with a ‘+’ cross through the center to the figure and needs x and y coordinates of its center.
  • circle_x() method is a used to add a circle glyph with a ‘X’ cross through the center. to the figure and needs x and y coordinates of its center.

Example:

Python3

import numpy as np 
from bokeh.plotting import figure, output_file, show 
  
# creating the figure object
plot = figure(plot_width = 300, plot_height = 300) 
  
plot.circle(x = [1, 2, 3], y = [3, 7, 5], size = 20) 
  
show(plot) 
                      
                       

Output:

bokeh tutorial circle

Refer to the below articles to get detailed information about the circle glyphs

  • Make an Circle Glyphs in Python using Bokeh

Oval

oval() method can be used to plot ovals on the graph.

Syntax:

oval(parameters)

Example:

Python3

# importing the modules 
from bokeh.plotting import figure, output_file, show 
  
      
# instantiating the figure object 
graph = figure(title = "Bokeh Oval Graph") 
      
# the points to be plotted 
x = [1, 2, 3, 4, 5] 
y = [i * 2 for i in x] 
  
# plotting the graph 
graph.oval(x, y, 
        height = 0.5, 
        width = 1) 
      
# displaying the model 
show(graph) 
                      
                       

Output:

boekh tutorial oval

Refer o the below articles to get detailed information about the oval glyphs.

  • Python Bokeh – Plotting Ovals on a Graph

Triangle

Triangle can be created using the triangle() method.

Syntax:

triangle(parameters)

Example:

Python3

# importing the modules 
from bokeh.plotting import figure, output_file, show 
  
  
# instantiating the figure object 
graph = figure(title = "Bokeh Triangle Graph") 
      
# the points to be plotted 
x = 1
y = 1
  
# plotting the graph 
graph.triangle(x, y, size = 150) 
      
# displaying the model 
show(graph)
                      
                       

Output:

Refer to the below article to get detailed information about the triangles.

  • Python Bokeh – Plotting Triangles on a Graph

Rectangle

Just like circles and ovals rectangle can also be plotted in Bokeh. It can be plotted using the rect() method.

Syntax:

rect(parameters)

Example:

Python3

# importing the modules 
from bokeh.plotting import figure, output_file, show 
  
          
# instantiating the figure object 
graph = figure(title = "Bokeh Rectangle Graph", match_aspect = True) 
  
# the points to be plotted 
x = 0
y = 0
width = 10
height = 5
  
# plotting the graph 
graph.rect(x, y, width, height) 
      
# displaying the model 
show(graph) 
                      
                       

Output:

bokeh tutorial rectanlge

Polygon

Bokeh can also be used to plot multiple polygons on a graph. Plotting multiple polygons on a graph can be done using the multi_polygons() method of the plotting module.

Syntax:

multi_polygons(parameters)

Example:

Python3

# importing the modules 
from bokeh.plotting import figure, output_file, show 
  
      
# instantiating the figure object 
graph = figure(title = "Bokeh Multiple Polygons Graph") 
      
# the points to be plotted 
xs = [[[[1, 1, 3, 4]]]] 
ys = [[[[1, 3, 2 ,1]]]] 
      
# plotting the graph 
graph.multi_polygons(xs, ys) 
      
# displaying the model 
show(graph) 
                      
                       

Output:

bokeh tutorial ploygon

Refer to the below articles to get detailed information about the polygon glyphs.

  • Python Bokeh – Plotting Multiple Polygons on a Graph

Plotting Multiple Plots

There are several layouts provided by the Bokeh in order to create Multiple Plots. These layouts are:

  • Vertical Layout
  • Horizontal Layout
  • Grid Layout

Vertical Layouts

Vertical Layout set all the plots in the vertical fashion and can be created using the column() method.

Python3

from bokeh.io import output_file, show
from bokeh.layouts import column
from bokeh.plotting import figure
  
  
x = [1, 2, 3, 4, 5, 6]
y0 = x
y1 = [i * 2 for i in x]
y2 = [i ** 2 for i in x]
  
# create a new plot
s1 = figure(width=200, plot_height=200)
s1.circle(x, y0, size=10, alpha=0.5)
  
# create another one
s2 = figure(width=200, height=200)
s2.triangle(x, y1, size=10, alpha=0.5)
  
# create and another
s3 = figure(width=200, height=200)
s3.square(x, y2, size=10, alpha=0.5)
  
# put all the plots in a VBox
p = column(s1, s2, s3)
  
# show the results
show(p)
                      
                       

Output:

bokeh tutorial column

Horizontal Layout 

Horizontal Layout set all the plots in the horizontal fashion. It can be created using the row() method.

Example:

Python3

from bokeh.io import output_file, show
from bokeh.layouts import row
from bokeh.plotting import figure
  
  
x = [1, 2, 3, 4, 5, 6]
y0 = x
y1 = [i * 2 for i in x]
y2 = [i ** 2 for i in x]
  
# create a new plot
s1 = figure(width=200, plot_height=200)
s1.circle(x, y0, size=10, alpha=0.5)
  
# create another one
s2 = figure(width=200, height=200)
s2.triangle(x, y1, size=10, alpha=0.5)
  
# create and another
s3 = figure(width=200, height=200)
s3.square(x, y2, size=10, alpha=0.5)
  
# put all the plots in a VBox
p = row(s1, s2, s3)
  
# show the results
show(p)
                      
                       

Output:

Bokeh Tutorial roe

Grid Layout

gridplot() method can be used to arrange all the plots in the grid fashion. we can also pass None to leave a space empty for a plot.

Example:

Python3

from bokeh.io import output_file, show
from bokeh.layouts import gridplot
from bokeh.plotting import figure
  
  
x = [1, 2, 3, 4, 5, 6]
y0 = x
y1 = [i * 2 for i in x]
y2 = [i ** 2 for i in x]
  
# create a new plot
s1 = figure()
s1.circle(x, y0, size=10, alpha=0.5)
  
# create another one
s2 = figure()
s2.triangle(x, y1, size=10, alpha=0.5)
  
# create and another
s3 = figure()
s3.square(x, y2, size=10, alpha=0.5)
  
# put all the plots in a grid
p = gridplot([[s1, None], [s2, s3]], plot_width=200, plot_height=200)
  
# show the results
show(p)
                      
                       

Output:

Bokeh tutorial grid

Interactive Data Visualization

One of the key feature of Bokeh which differentiate it from other visualizing libraries is adding interaction to the Plot. Let’s see various interactions that can be added to the plot.

Configuring Plot Tools

In all the above graphs you must have noticed a toolbar that appears mostly at the right of the plot. Bokeh provides us the methods to handle these tools. Tools can be classified into four categories.

  • Gestures: These tools handle the gestures such as pan movement. There are three types of gestures:
    • Pan/Drag Tools
    • Click/Tap Tools
    • Scroll/Pinch Tools
  • Actions: These tools handle when a button is pressed.
  • Inspectors: These tools report information or annotate the graph such as  HoverTool.
  • Edit Tools: These are multi gestures tools that can add, delete glyphs from the graph.

Adjusting the Position of the ToolBar

We can specify the position of the toolbar according to our own needs. It can be done by passing the toolbar_location parameter to the figure() method. The possible value to this parameter is – 

  • “above”
  • “below”
  • “left”
  • “right”

Example: 

Python3

# importing the modules 
from bokeh.plotting import figure, output_file, show 
  
# instantiating the figure object 
graph = figure(title = "Bokeh ToolBar", toolbar_location="below") 
  
# the points to be plotted 
x = [1, 2, 3, 4, 5] 
y = [1, 2, 3, 4, 5]  
  
# height / thickness of the plot
width = 0.5
  
# plotting the scatter graph 
graph.scatter(x, y) 
  
# displaying the model 
show(graph)
                      
                       

Output:

Bokeh Tutorial toolbar

Interactive Legends

In the section annotations and legends we have seen the list of all the parameters of the legends, however, we have not discussed the click_policy parameter yet. This property makes the legend interactive. There are two types of interactivity –

  • Hiding: Hides the Glyphs.
  • Muting: Hiding the glyph makes it vanish completely, on the other hand, muting the glyph just de-emphasizes the glyph based on the parameters.

Example 1: Hiding the legend

Python3

# importing the modules 
from bokeh.plotting import figure, output_file, show 
  
# file to save the model 
output_file("gfg.html") 
          
# instantiating the figure object 
graph = figure(title = "Bokeh Hiding Glyphs") 
  
# plotting the graph 
graph.vbar(x = 1, top = 5, 
        width = 1, color = "violet", 
        legend_label = "Violet Bar") 
graph.vbar(x = 2, top = 5, 
        width = 1, color = "green", 
        legend_label = "Green Bar") 
graph.vbar(x = 3, top = 5, 
        width = 1, color = "yellow", 
        legend_label = "Yellow Bar") 
graph.vbar(x = 4, top = 5, 
        width = 1, color = "red", 
        legend_label = "Red Bar") 
  
# enable hiding of the glyphs 
graph.legend.click_policy = "hide"
  
# displaying the model 
show(graph) 
                      
                       

Output:

bokeh tutorial hiding legend

Example 2: Muting the legend

Python3

# importing the modules 
from bokeh.plotting import figure, output_file, show 
  
# file to save the model 
output_file("gfg.html") 
          
# instantiating the figure object 
graph = figure(title = "Bokeh Hiding Glyphs") 
  
# plotting the graph 
graph.vbar(x = 1, top = 5, 
        width = 1, color = "violet", 
        legend_label = "Violet Bar", 
        muted_alpha=0.2) 
graph.vbar(x = 2, top = 5, 
        width = 1, color = "green", 
        legend_label = "Green Bar", 
        muted_alpha=0.2) 
graph.vbar(x = 3, top = 5, 
        width = 1, color = "yellow", 
        legend_label = "Yellow Bar", 
        muted_alpha=0.2) 
graph.vbar(x = 4, top = 5, 
        width = 1, color = "red", 
        legend_label = "Red Bar", 
        muted_alpha=0.2) 
  
# enable hiding of the glyphs 
graph.legend.click_policy = "mute"
  
# displaying the model 
show(graph) 
                      
                       

Output:

bokeh tutorial muting legend

Adding Widgets to the Plot

Bokeh provides GUI features similar to HTML forms like buttons, slider, checkbox, etc. These provide an interactive interface to the plot that allows to change the parameters of the plot, modifying plot data, etc. Let’s see how to use and add some commonly used widgets. 

  • Buttons: This widget adds a simple button widget to the plot. We have to pass a custom JavaScript function to the CustomJS() method of the models class.

Syntax:

Button(label, icon, callback)

Example:

Python3

from bokeh.io import show
from bokeh.models import Button, CustomJS
  
button = Button(label="GFG")
button.js_on_click(CustomJS(
  code="console.log('button: click!', this.toString())"))
  
show(button)
                      
                       

Output: 

bokeh tutorial button
  • CheckboxGroup: Adds a standard check box to the plot. Similarly to buttons we have to pass the custom JavaScript function to the CustomJS() method of the models class.

Example:

Python3

from bokeh.io import show
from bokeh.models import CheckboxGroup, CustomJS
  
L = ["First", "Second", "Third"]
  
# the active parameter sets checks the selected value 
# by default
checkbox_group = CheckboxGroup(labels=L, active=[0, 2])
  
checkbox_group.js_on_click(CustomJS(code="""
    console.log('checkbox_group: active=' + this.active, this.toString())
"""))
  
show(checkbox_group)
                      
                       

Output:

Bokeh tutorial check box
  • RadioGroup: Adds a simple radio button and accepts a custom JavaScript function.

Syntax:

RadioGroup(labels, active)

Example:

Python3

from bokeh.io import show
from bokeh.models import RadioGroup, CustomJS
  
L = ["First", "Second", "Third"]
  
# the active parameter sets checks the selected value 
# by default
radio_group = RadioGroup(labels=L, active=1)
  
radio_group.js_on_click(CustomJS(code="""
    console.log('radio_group: active=' + this.active, this.toString())
"""))
  
show(radio_group)
                      
                       

Output:

Bokeh Tutorial radio button
  • Sliders: Adds a slider to the plot. It also needs a custom JavaScript function.

Syntax:

Slider(start, end, step, value)

Example:

Python3

from bokeh.io import show
from bokeh.models import CustomJS, Slider
  
slider = Slider(start=1, end=20, value=1, step=2, title="Slider")
  
slider.js_on_change("value", CustomJS(code="""
    console.log('slider: value=' + this.value, this.toString())
"""))
  
show(slider)
                      
                       

Output:

bokeh tutorial slider
  • DropDown: Adds a dropdown to the plot and like every other widget it also needs a custom JavaScript function as callback.

Example:

Python3

from bokeh.io import show
from bokeh.models import CustomJS, Dropdown
  
menu = [("First", "First"), ("Second", "Second"), ("Third", "Third")]
  
dropdown = Dropdown(label="Dropdown Menu", button_type="success", menu=menu)
  
dropdown.js_on_event("menu_item_click", CustomJS(
    code="console.log('dropdown: ' + this.item, this.toString())"))
  
show(dropdown)
                      
                       

Output:

bokeh tutorial dropdown
  • Tab Widget: Tab Widget adds tabs and each tab show a different plot.

Example:

Python3

from bokeh.plotting import figure, output_file, show
from bokeh.models import Panel, Tabs
import numpy as np
import math
  
  
fig1 = figure(plot_width=300, plot_height=300)
  
x = [1, 2, 3, 4, 5]
y = [5, 4, 3, 2, 1]
  
fig1.line(x, y, line_color='green')
tab1 = Panel(child=fig1, title="Tab 1")
  
fig2 = figure(plot_width=300, plot_height=300)
  
fig2.line(y, x, line_color='red')
tab2 = Panel(child=fig2, title="Tab 2")
  
all_tabs = Tabs(tabs=[tab1, tab2])
  
show(all_tabs)
                      
                       

Output:

bokeh tutorial tabs

Creating Different Types of Glyphs

  • Diamond Glyph
  • Dash Glyph
  • Cross Glyph
  • Diamond Glyph
  • Diamond Cross Glyph
  • Diamond Dot Glyph
  • Ray Glyph
  • Triangle Pins Glyph
  • Triangle with Dots Glyph
  • Inverted Triangle Glyph
  • Plus Glyph
  • Quadrilateral Glyph
  • Y Glyph
  • X Glyph
  • Oval Glyph
  • Hexagon Glyph
  • Hexagon Tiles Glyph
  • Hexagon Dots Glyph
  • Square with Cross Glyph
  • Square with Dots Glyph
  • Square with X’s Glyph
  • Ellipse glyph
  • Annulus Glyph
  • Bezier Glyph
  • Asterisk Glyph
  • AnnularWedge Glyph
  • Step Glyph

Visualizing Different Types of Data

  • Python Bokeh – Visualizing the Iris Dataset
  • Python Bokeh – Visualizing Stock Data
  • Interactive visualization of data using Bokeh

More Topics on Bokeh

  • Python Bokeh – Colors Class
  • How to use Color Palettes in Python-Bokeh?
  • Python Bokeh – Plotting Quadratic Curves on a Graph
  • Python Bokeh – Plotting Line Segments on a Graph
  • Python Bokeh – Plotting Multiple Lines on a Graph
  • Python Bokeh – Plotting glyphs over a Google Map
  • Python Bokeh – Plot for all Types of Google Maps ( roadmap, satellite, hybrid, terrain)


Next Article
Introduction to Bokeh in Python
author
abhishek1
Improve
Article Tags :
  • AI-ML-DS
  • Data Visualization
  • AI-ML-DS With Python
  • Python Data Visualization
  • Python-Bokeh

Similar Reads

  • Python Bokeh tutorial - Interactive Data Visualization with Bokeh
    Python Bokeh is a Data Visualization library that provides interactive charts and plots. Bokeh renders its plots using HTML and JavaScript that uses modern web browsers for presenting elegant, concise construction of novel graphics with high-level interactivity.  Features of Bokeh: Flexibility: Boke
    15+ min read
  • Getting started With Bokeh

    • Introduction to Bokeh in Python
      Bokeh is a Python interactive data visualization. Unlike Matplotlib and Seaborn, Bokeh renders its plots using HTML and JavaScript. It targets modern web browsers for presentation providing elegant, concise construction of novel graphics with high-performance interactivity. Features of Bokeh: Some o
      1 min read

    • How to Install Python Bokeh Library on Windows?
      There are different types of data visualization modules in Python like Matplotlib, Seaborn, or Plotly among them Bokeh module is one which is used to capsulate information or data in the form of graphs and charts which are embedded in flask and Django applications. This module is also used for makin
      2 min read

    • How to Install Bokeh in Python3 on MacOS?
      Data visualization is the graphical representation of information and data with the help of charts and graphs. There are different types of well-known data visualization libraries like Matplotlib, Seaborn or Plotly for presenting information and data in the form of charts and graphs. Bokeh is also a
      2 min read

    • Python - Setting up the Bokeh Environment
      Bokeh is supported on CPython versions 3.6+ only both with Standard distribution and Anaconda distribution. Other Python versions or implementations may or may not function. Current version of Bokeh is 2.0.2 . Bokeh package has the following dependencies: 1. Required Dependencies PyYAML>=3.10pyth
      1 min read

    Plotting Different Types of Plots

    • Python Bokeh - Plotting Vertical Bar Graphs
      Bokeh is a Python interactive data visualization. It renders its plots using HTML and JavaScript. It targets modern web browsers for presentation providing elegant, concise construction of novel graphics with high-performance interactivity.Bokeh can be used to plot vertical bar graphs. Plotting vert
      4 min read

    • Python Bokeh - Plotting a Scatter Plot on a Graph
      Bokeh is a Python interactive data visualization. It renders its plots using HTML and JavaScript. It targets modern web browsers for presentation providing elegant, concise construction of novel graphics with high-performance interactivity. Bokeh can be used to plot a scatter plot on a graph. Plotti
      2 min read

    • Python Bokeh - Plotting Patches on a Graph
      Bokeh is a Python interactive data visualization. It renders its plots using HTML and JavaScript. It targets modern web browsers for presentation providing elegant, concise construction of novel graphics with high-performance interactivity. Bokeh can be used to plot patches on a graph. Plotting patc
      2 min read

    • Make an area plot in Python using Bokeh
      Bokeh is a Python interactive data visualization. Unlike Matplotlib and Seaborn, Bokeh renders its plots using HTML and JavaScript. It targets modern web browsers for presentation providing elegant, concise construction of novel graphics with high-performance interactivity. Plotting the Area Plots A
      2 min read

    • Python Bokeh - Making a Pie Chart
      Bokeh is a Python interactive data visualization. It renders its plots using HTML and JavaScript. It targets modern web browsers for presentation providing elegant, concise construction of novel graphics with high-performance interactivity. Let us see how to plot a pie chart in Bokeh. Does not provi
      3 min read

    Annotations and Legends

    • Python Bokeh - Making Interactive Legends
      Bokeh is a Python interactive data visualization. It renders its plots using HTML and JavaScript. It targets modern web browsers for presentation providing elegant, concise construction of novel graphics with high-performance interactivity. How to make Interactive legends? The legend of a graph refl
      2 min read

    • Bokeh - Annotations and Legends
      Prerequisites: Bokeh Bokeh includes several types of annotations to allow users to add supplemental information to their visualizations. Annotations are used to add notes or more information about a topic. Annotations can be titles, legends, Arrows, bands, labels etc. Adding legends to your figures
      2 min read

    Creating Diffrent Shapes

    • Python Bokeh - Plotting Ovals on a Graph
      Bokeh is a Python interactive data visualization. It renders its plots using HTML and JavaScript. It targets modern web browsers for presentation providing elegant, concise construction of novel graphics with high-performance interactivity. Bokeh can be used to plot ovals on a graph. Plotting ovals
      4 min read

    • Python Bokeh - Plotting Triangles on a Graph
      Bokeh is a Python interactive data visualization. It renders its plots using HTML and JavaScript. It targets modern web browsers for presentation providing elegant, concise construction of novel graphics with high-performance interactivity. Bokeh can be used to plot triangles on a graph. Plotting tr
      2 min read

    • Python Bokeh - Plotting Multiple Polygons on a Graph
      Bokeh is a Python interactive data visualization. It renders its plots using HTML and JavaScript. It targets modern web browsers for presentation providing elegant, concise construction of novel graphics with high-performance interactivity.Bokeh can be used to plot multiple polygons on a graph. Plot
      3 min read

    • Python Bokeh - Plotting Rectangles on a Graph
      Bokeh is a Python interactive data visualization. It renders its plots using HTML and JavaScript. It targets modern web browsers for presentation providing elegant, concise construction of novel graphics with high-performance interactivity. Bokeh can be used to plot rectangles on a graph. Plotting r
      2 min read

    Plotting Multiple Plots

    • Bokeh - Vertical layout of plots
      Bokeh includes several layout options for arranging plots and widgets. They make it possible to arrange multiple components to create interactive data applications. The layout functions helps build a grid of plots and widgets. It supports nesting of as many rows, columns, or grids of plots together
      2 min read

    • Bokeh - Horizontal layout of plots
      Bokeh includes several layout options for arranging plots and widgets. They make it possible to arrange multiple components to create interactive dashboards or data applications. The layout functions let you build a grid of plots and widgets. You can nest as many rows, columns, or grids of plots tog
      2 min read

    • Bokeh - grid layout of plots
      Bokeh includes several layout options for arranging plots and widgets. They make it possible to arrange multiple components to create interactive dashboards or data applications. The layout functions let you build a grid of plots and widgets. You can nest as many rows, columns, or grids of plots tog
      5 min read

    Functions in Bokeh

    • bokeh.plotting.figure.cross() function in Python
      Bokeh is a data visualization library in Python that provides high-performance interactive charts and plots and the output can be obtained in various mediums like notebook, HTML, and server. Figure Class create a new Figure for plotting. It is a subclass of Plot that simplifies plot creation with de
      2 min read

    • bokeh.plotting.figure.diamond_cross() function in Python
      Bokeh is a data visualization library in Python that provides high-performance interactive charts and plots and the output can be obtained in various mediums like a notebook, HTML and server. Figure Class create a new Figure for plotting. It is a subclass of Plot that simplifies plot creation with d
      2 min read

    • bokeh.plotting.figure.step() function in Python
      Bokeh is a data visualization library in Python that provides high-performance interactive charts and plots and the output can be obtained in various mediums like notebook, html and server. The Figure Class create a new Figure for plotting. It is a subclass of Plot that simplifies plot creation with
      4 min read

    • bokeh.plotting.figure.circle_cross() function in Python
      Bokeh is a data visualization library in Python that provides high-performance interactive charts and plots and the output can be obtained in various mediums like notebook, html and server. The Figure Class create a new Figure for plotting. It is a subclass of Plot that simplifies plot creation with
      4 min read

    • bokeh.plotting.figure.annular_wedge() function in Python
      Bokeh is a data visualization library in Python that provides high-performance interactive charts and plots and the output can be obtained in various mediums like notebook, html and server. The Figure Class create a new Figure for plotting. It is a subclass of Plot that simplifies plot creation with
      4 min read

    • bokeh.plotting.figure.arc() function in Python
      Bokeh is a data visualization library in Python that provides high-performance interactive charts and plots and the output can be obtained in various mediums like notebook, html and server. The Figure Class create a new Figure for plotting. It is a subclass of Plot that simplifies plot creation with
      4 min read

    • bokeh.plotting.figure.asterisk() function in Python
      Bokeh is a data visualization library in Python that provides high-performance interactive charts and plots and the output can be obtained in various mediums like notebook, html and server. The Figure Class create a new Figure for plotting. It is a subclass of Plot that simplifies plot creation with
      4 min read

    • bokeh.plotting.figure.bezier() function in Python
      Bokeh is a data visualization library in Python that provides high-performance interactive charts and plots and the output can be obtained in various mediums like notebook, html and server. The Figure Class create a new Figure for plotting. It is a subclass of Plot that simplifies plot creation with
      4 min read

    • bokeh.plotting.figure.circle_x() function in Python
      Bokeh is a data visualization library in Python that provides high-performance interactive charts and plots and the output can be obtained in various mediums like notebook, html and server. The Figure Class create a new Figure for plotting. It is a subclass of Plot that simplifies plot creation with
      4 min read

    • bokeh.plotting.figure.circle() function in Python
      Bokeh is a data visualization library in Python that provides high-performance interactive charts and plots and the output can be obtained in various mediums like notebook, html and server. The Figure Class create a new Figure for plotting. It is a subclass of Plot that simplifies plot creation with
      4 min read

    • bokeh.plotting.figure.annulus() function in Python
      Bokeh is a data visualization library in Python that provides high-performance interactive charts and plots and the output can be obtained in various mediums like notebook, html and server. The Figure Class create a new Figure for plotting. It is a subclass of Plot that simplifies plot creation with
      4 min read

    Interactive Data Visualization

    • Configuring Plot Tooltips in Bokeh
      Bokeh is a powerful data visualization library in Python that allows you to create interactive and visually appealing plots. The Bokeh plotting module provides several tools that can be used to enhance the functionality of the plots. These tools can be configured to suit your specific needs. In this
      4 min read

    • Bokeh - Adding Widgets
      Bokeh is a Python data visualization library for creating interactive charts & plots. It helps us in making beautiful graphs from simple plots to dashboards. Using this library, we can create javascript-generated visualization without writing any scripts. What is a widget? Widgets are interactiv
      11 min read

    Graph

    • Python Bokeh - Plotting a Line Graph
      Bokeh is a Python interactive data visualization. It renders its plots using HTML and JavaScript. It targets modern web browsers for presentation providing elegant, concise construction of novel graphics with high-performance interactivity. Bokeh can be used to plot a line graph. Plotting a line gra
      4 min read

    • Python Bokeh - Plotting Multiple Lines on a Graph
      Bokeh is a Python interactive data visualization. It renders its plots using HTML and JavaScript. It targets modern web browsers for presentation providing elegant, concise construction of novel graphics with high-performance interactivity. Bokeh can be used to plot multiple lines on a graph. Plotti
      3 min read

    • Python Bokeh - Plotting Horizontal Bar Graphs
      Bokeh is a Python interactive data visualization. It renders its plots using HTML and JavaScript. It targets modern web browsers for presentation providing elegant, concise construction of novel graphics with high-performance interactivity. Bokeh can be used to plot horizontal bar graphs. Plotting h
      4 min read

    • Python Bokeh - Plotting Vertical Bar Graphs
      Bokeh is a Python interactive data visualization. It renders its plots using HTML and JavaScript. It targets modern web browsers for presentation providing elegant, concise construction of novel graphics with high-performance interactivity.Bokeh can be used to plot vertical bar graphs. Plotting vert
      4 min read

    • Python Bokeh - Plotting a Scatter Plot on a Graph
      Bokeh is a Python interactive data visualization. It renders its plots using HTML and JavaScript. It targets modern web browsers for presentation providing elegant, concise construction of novel graphics with high-performance interactivity. Bokeh can be used to plot a scatter plot on a graph. Plotti
      2 min read

    • Python Bokeh - Plotting Patches on a Graph
      Bokeh is a Python interactive data visualization. It renders its plots using HTML and JavaScript. It targets modern web browsers for presentation providing elegant, concise construction of novel graphics with high-performance interactivity. Bokeh can be used to plot patches on a graph. Plotting patc
      2 min read

    • Make an area plot in Python using Bokeh
      Bokeh is a Python interactive data visualization. Unlike Matplotlib and Seaborn, Bokeh renders its plots using HTML and JavaScript. It targets modern web browsers for presentation providing elegant, concise construction of novel graphics with high-performance interactivity. Plotting the Area Plots A
      2 min read

    • Python Bokeh - Plotting Wedges on a Graph
      Bokeh is a Python interactive data visualization. It renders its plots using HTML and JavaScript. It targets modern web browsers for presentation providing elegant, concise construction of novel graphics with high-performance interactivity. Bokeh can be used to plot wedges on a graph. Plotting wedge
      3 min read

    • Python Bokeh - Making a Pie Chart
      Bokeh is a Python interactive data visualization. It renders its plots using HTML and JavaScript. It targets modern web browsers for presentation providing elegant, concise construction of novel graphics with high-performance interactivity. Let us see how to plot a pie chart in Bokeh. Does not provi
      3 min read

    • Python Bokeh - Plotting Triangles on a Graph
      Bokeh is a Python interactive data visualization. It renders its plots using HTML and JavaScript. It targets modern web browsers for presentation providing elegant, concise construction of novel graphics with high-performance interactivity. Bokeh can be used to plot triangles on a graph. Plotting tr
      2 min read

    • Python Bokeh - Plotting Ovals on a Graph
      Bokeh is a Python interactive data visualization. It renders its plots using HTML and JavaScript. It targets modern web browsers for presentation providing elegant, concise construction of novel graphics with high-performance interactivity. Bokeh can be used to plot ovals on a graph. Plotting ovals
      4 min read

    Building Advanced Visualizations with Glyphs

    • Glyphs in Bokeh
      Bokeh is a library of Python which is used to create interactive data visualizations. In this article, we will discuss glyphs in Bokeh. But at first let's see how to install Bokeh in Python. Installation To install this type the below command in the terminal. conda install bokeh Or pip install bokeh
      6 min read

    • Create a plot with Multiple Glyphs using Python Bokeh
      In this article, we will be learning about multiple glyphs and also about adding a legend in bokeh. Now bokeh provides us with a variety of glyphs that can be used to represent a point in a plot. Some glyphs are circle, square, asterik, inverted_triangle(), triangle() etc. Installation This module d
      7 min read

    • Make an Circle Glyphs in Python using Bokeh
      Bokeh is a Python interactive data visualization. Unlike Matplotlib and Seaborn, Bokeh renders its plots using HTML and JavaScript. It targets modern web browsers for presentation providing elegant, concise construction of novel graphics with high-performance interactivity. Plotting the Circle Glyph
      4 min read

geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences