Data Visualization in Bokeh – Line Graph

Almost one and half year back I create a data visualization series with Matplotlib which was appreciated by hundreds of developers. Today I am going to start another data visualization series to create interactive graphs and charts in Bokeh.

What is Bokeh

From the official website:

Bokeh is an interactive visualization library that targets modern web browsers for presentation. Its goal is to provide elegant, concise construction of versatile graphics, and to extend this capability with high-performance interactivity over very large or streaming datasets. Bokeh can help anyone who would like to quickly and easily create interactive plots, dashboards, and data applications.

In short, Bokeh allows Python developers to create interactive charts that are rendered as HTML and Javascript.

Why Bokeh?

You might be wondering why do you need Bokeh when Matplotlib is already around and doing well? As I mentioned above, Bokeh gives you the opportunity to create interactive graphs that could provide more information than static graphs which are generated in Matplotlib. On top of it, you can use the generated graph output in an HTML document which is rendered on standard browsers, thus it gives options to non- developers to use the graph to professionals like journalists to describe their stories. Interactive graphs got popular after D3.js library but the learning curve is steep hence many developers are not able to use it easily.

Development Setup

Installation of Bokeh library is easy. The best way to install it by using pip command:

pip install bokeh

pip will take care of dependencies itself so you don’t need to get worried about it.

Now we are going to work on our first program that will generate a line graph.

After importing the necessities I called output_file() method which takes a file name as a parameter. What actually it does that it dump the graph output in the given HTML file. The figure() method lays the ground for your creativity. I just took a few even and odd data points here. The show() method takes plot object as a parameter and shows the file in the browser. We also set title and labels for both X and Y axis. When you simply call output_file() with file name only, it loads both CSS and JS from a remote CDN. If you open line.html you will see something like this:

It takes time to load, in order to load both CSS and JS locally you will add mode=inline parameter in output_file(). Once you do, all the JS and CSS then loaded inline hence loads easily.

Let’s work on real data. In the matplotlib related post for a line graph, I used the population data of India and Pakistan. I will be using the same data again.

I took data from the previous graph related post. After setting of the file name, title, etc, I called plot.line twice as we have two countries’ data. I also set the legend and color thees etc. Once run it will open an HTML file in the browser.

Looks good, pretty much similar to what I did with Matplotlib. Can you notice the tools on the top right, they can help to zoom in/out, saving the graph in image format and a few others. Let’s add points now in graph so that we can know actual data points.

Once run you can see the circles indicating the data points.

 

Looks much better. How about make it interactive by showing information on hover? Before we do that, let’s discuss a bit about ColumnDataSource.

ColumnDataSource(CDS) is a data structure Bokeh uses to render graphs. You can use python dictionaries and list too for assigning data but you are not able to use features like showing information on Hover is not available.

I created two CDS objects and assigned value to keys. We will then use it in plot object.

I passed two different CDS for different countries. The result would be the same as above. Now I want to show information whenever someone hovers the mouse on data points. For that, we will use HoverTool object.

Do you notice @ with string? This is used to access the key/value store in CDS. I then made Hovertool the part of plot object by calling add_tools method. When you run it now, it shows the following information on hover.

 

Conclusion

In this post, you learned how to install Bokeh and how you can easily create interactive graphs in it by writing a few lines of code. In coming posts I will be discussing other kind of graphs that you can create in Bokeh. The code is available on Github.

 

 

If you like this post then you should subscribe to my blog for future updates.

* indicates required