How do you visualize data using Bokeh?

How do you visualize data using Bokeh?

Bokeh is an interactive data visualization package that is used in python that adds a layer of interactivity to your plots. This guide will show you how you can add different layers of interactivity to your bokeh plots which is an essential weapon in the arsenal of any data scientist who works directly with the application.

The first step is to understand how we can import the necessary modules so that we can work with Bokeh on Jupyter Notebook. We can do this using the code shown below:

Next let’s construct a basic scatter plot with bokeh using the code shown below:

The code above first imports the necessary module from bokeh. We then define a variable called ‘scatter’ which uses the figure() function in order to plot a figure having a width of 300. The ‘tools’ argument is used to add interactivity to your plots. Here we are using ‘pan’ and ‘box_zoom’ as our layers of interactivity. ‘Pan’ lets drag our mouse over the plot in order to highlight different areas of the plot while ‘box_zoom’ lets you select a box of area and zoom over that.

The output of the plot is illustrated below:

On the right hand side of the plot you can see the tools that you have configured while writing your code. The first tool which resembles the + symbol is the pan tool while the tool representing a magnifying glass is the box_zoom tool. You can use these tools by clicking on them in the plot.

We can also configure the size of the points or circles in our scatter plot using the code shown below:

This results in an output which is illustrated below:

You don’t have to limit your points to circles. If you use scatter.diamond() your points on the graph would be diamond shaped. Refer the documentation on Bokeh to find out the different kinds of shapes available to you to plot your point.

You can use bokeh in order to plot line plots with the code shown below:

The resulting plot looks something like this: 

Notice how the line_plot.diamond() produced diamond shaped markers for the points we specified.

Bokeh can also be implemented to visualize plots using a pandas data frame. Before we can use the the pandas data frame to visualize a Bokeh plot we need to first convert the data frame into a ColumnDataSource() – which is a type of object that is compatible with bokeh plots. The code below illustrates how we can convert a data frame into a CDS:

In the code above we first import the required packages. We then read in the data frame of interest using the pandas read_csv method. Next, we use the ColumnDataSource() function in order to convert the ‘ WhiteMeat’ and ‘ RedMeat’ columns of our data frame into a CDS object.

Next, let’s write code in order to plot a simple scatter plot between the protein values of red meat and white meat. We can do this using the code shown below:

This results in a plot as shown below: 

The scatterplot above has a range of interactive features that you can use ranging from a box zoom, reset, drag and scroll and even a save option.

What if we wanted to identify the exact values of the x and y points when we point our mouse at a particular point of interest? We can achieve this by using the ‘hover’ tool provided by Bokeh. In order to add a hover tool to the plot above we use the code shown below:

In the code above we first import the curdoc package and add a Hover tool using the HoverTool() function. Inside the function we specify the [‘Value_Name’, @column] pair. The ‘Value_Name’ is arbitrary and depends on what name we want displayed beside the value. The @column is the column name that we defined earlier in the ColumnDataSource() function. In the code above the @column was ‘x’ and ‘y’ because that is what we assigned while defining the columns of ‘ WhiteMeat’ and ‘ RedMeat’ respectively. This results in a plot as shown below:

We can see how when we pointed our mouse at a point it displays the corresponding x and y values.

Bokeh is an amazing interactive data visualization package that is managed by an active community which develops and adds new features to it everyday!

For a brief overview of all the features available to you while plotting with Bokeh – This link should help you.

Have fun experimenting with everything that Bokeh has to offer you!

Happy Bokeh Plotting!



  1. Hey there! Someone in my Myspace group shared this website with us so I came to give it a look. I’m definitely enjoying the information. I’m book-marking and will be tweeting this to my followers! Excellent blog and great design.

  2. Thank you a lot for providing individuals with such a pleasant opportunity to check tips from this website. It is usually very terrific plus jam-packed with a good time for me personally and my office colleagues to search your web site at minimum three times in 7 days to read through the latest guidance you have. And definitely, I’m so at all times astounded concerning the incredible opinions served by you. Some 4 tips in this post are truly the simplest we’ve had.

  3. I believe that is one of the such a lot important information for me.

    And i’m satisfied reading your article. However wanna observation on few general things, The site style is ideal,
    the articles is in point of fact nice : D. Excellent activity, cheers