A Beginner’s Journey Into Bokeh (Python focus)

Jonathan Beatty
5 min readOct 9, 2018

--

When I began our project on the classification of West Nile Virus based from the Kaggle competition, I had no idea it would lead me to learning a new visualization library! This light look at Bokeh is based on a lightning talk I gave which I will link to at the end of the blog. This blog post is based on a MUCH more inclusive and extensive blog post on Bokeh, check it out for more inspiration!

First of all, what is Bokeh?

  • Bokeh is a Python library for interactive visualization that can target web browsers for representation.
  • You can pass data of all sorts, such as Python lists or tuples, NumPy arrays or Pandas DataFrames to make your plots.
  • Other languages supported by Bokeh: Bokeh for R and Bokeh for Scala

Why use Bokeh?

Given how many visualization libraries are out there why would you use Bokeh? There are a few key features that I believe make it extremely useful in key situations:

  • Interactivity: Your plots and graphs can be embedded into html so that they can be fully interactive on other web pages
  • Ease of representation online: It’s extremely easy (simply using one line of code) to display your chart and graphs online.
  • Complex statistical plots quickly through simple commands.
  • Can transform visualizations from matplotlib, seaborn and ggplot.

The Install:

For those who don’t want to bother checking out the Bokeh Documentation the installation of Bokeh couldn’t be easier, the two examples below should easily set this up for use.

for the conda installation
for a simple pip approach (don’t forget the ! before pip in jupyter notebooks!)

Now we are rolling with Bokeh! Next thing to understand is the different levels of Bokeh. There are 3 different “levels” that allow the user to interact in a variety of ways, some allowing more control and customization and others allowing simple easy plotting and graphing, there’s something for everyone.

Outputting Bokeh:

As I mentioned above, one of the great features of Bokeh is the ease in which it can be ouputted to various formats for visualization. There are 3 ways to output Bokeh models:

  1. output_file for generating simple standalone HTML documents for Bokeh visualizations.
  2. output_notebook for displaying Bokeh visualizations inline in Jupyter/Zeppelin notebook cells.
  3. Server for people using the low level model API of Bokeh they can host their images on the custom Bokeh servers.

The three shades of Bokeh:

  1. Charts: High level use of Bokeh used to build complex statistical plots, quickly. This is very comparable to the functionality you would get with matplotlib with Python. The plus side to using this as opposed to matplotlib or another visualization library is the idea of simple outputting to .html for viewing online. Below is the code for creating a simple line chart.

2. Plotting: Mid level use of Bokeh. Centered around composing visual glyphs and building plots on one another. This was very useful in my project as I learned who to plot multiple different data frames information onto an interactive map of Chicago. We will review the code and plots in just a second!

3. Models: Low level of Bokeh. This is best used for advanced users or developers who want even more control over the creation of glyphs and all aspects of Bokeh.

Let’s check out some plotting!

We are going to run through a few images of the creation of my map using Bokeh’s Plotting API.

This code creates the image below, a standard “tile” of the Chicago area
Basic tile of Chicago area that we can now plot on

Now we will begin to dive a little deeper. In Bokeh there is a concept called Glyphs which are the basic visual building blocks of Bokeh plots, e.g. lines, rectangles, squares, wedges, patches, etc. The bokeh.plotting interface provides a convenient way to create plots centered around glyphs.

This code shows us instantiating the figure from before and then graphing the Circle and Square glyphs onto the map
They aren’t spraying in the right locations!!!

Lastly I added in some magnitude into the map using the ColumnDataSource function to map values from a pandas dictionary or data frame into the graph.

Notice the import statements and layout of the ColumnDataSource
Pretty nifty!

Cheat Sheet:

Below is a cheat sheet created by Data Camp that does a great job giving you a basic overview of the Bokeh commands.

Bokeh Application Server:

Now that you’ve seen some plotting in action, the last couple notes I wanted to put in are on the Bokeh Application Server for those who wish to explore it more. Bokeh also comes with an optional server component, the Bokeh Server, below is a sample of uses for the server:

  • UI widgets and plot selections driving computations and plot updates.
  • Intelligent server-side downsampling of large datasets.
  • Streaming data automatically updating plots.
  • Sophisticated glyph re-writing and transformations for “Big Data”.
  • Plot and dashboard publishing for wider audiences.

Thank you very much for reading and I hope you enjoyed this very basic overview of Bokeh and that it inspires you to try your hand at this wonderful visualization library yourself!

Resources:
1. Cheat sheet:
https://www.datacamp.com/community/blog/bokeh-cheat-sheet-python)
2. Main Bokeh Documentation:
https://bokeh.pydata.org/en/latest/
3. Original blog post I pulled the Chicago map info from:
https://towardsdatascience.com/data-visualization-with-bokeh-in-python-part-one-getting-started-a11655a467d4
4. Github repo:
https://github.com/JonathanBeatty/Bokeh_Lightning_Talk

--

--

Jonathan Beatty

Former Chef turned Data Scientist hoping to make an impact in a new field.