Elevate Your Data Visualization Game with Bokeh, a web graphing library in Python
Data Visualization is an integral part of the Data Science process. It is useful especially to derive information and insights in a faster, more lucid manner. Personally, I feel creating visuals to be one of the best parts of working with data, as this is the time when our true creativity and inner artistry is unshackled. According to the problem at hand, we have a wide array of options to choose from in terms of building said visuals, and thanks to Python’s rich ecosystem of libraries, we have many tools at our disposal.
In today’s day and age, I believe matplotlib.pyplot or the ‘plt’ alias and seaborn’s ‘sns’ alias have become household names in the Data Science community. Even if you’re at the very beginning of your data driven journey, I’m sure your notebooks are interspersed with these two entities. There is also a third contender, one becoming very popular due to its glossy plots and interactive features, or ‘callbacks’. Yes, you guessed it right, its Plotly. This beautiful graphing library is completely open source and has an enterprise version as well which offers diverse capabilities with its Dash framework. All of these are great tools and as such we must have some degree of familiarity with them. But the tool I’d like to discuss today distinguishes itself from all of the above in its approach and design.
Imagine a library that lets you write the complete driver code in Python and renders all of your plots on the browser using JavaScript, what’s more is that every plot you make has its own set of tools and options which you can use to interact with the plotted data in real time! and that too without you having to write a single piece of code in JS. Does this sound too good to be true? then get ready to be blown away because there is indeed such a library and it’s none other than Bokeh. According to Bokeh’s own documentation:
Bokeh is a Python library for creating interactive visualizations for modern web browsers. It helps you build beautiful graphics, ranging from simple plots to complex dashboards with streaming datasets. With Bokeh, you can create JavaScript-powered visualizations without writing any JavaScript yourself.
It works in a two-layered fashion, one is the Python library that lets us create all of the code in Python and the second is a JavaScript library called BokehJS which does all the heavy lifting for us, i.e., creating JS and HTML code for the plots and making sure they look beautiful and elegant whenever rendered. That’s all for now as a quick introduction to Bokeh, if you’d like to read more consider visiting their website and following them here Bokeh.
It is time now to get started with how you can start making plots using Bokeh library. First of all, let us install it using pip command.
pip install bokeh
You should see an output as shown below:
Once this is done, open your favorite code editor and start typing the below code:
from bokeh.plotting import figure,show
#set some value for the data points
x = [0,1,2,3]
y = [4,7,9,12]
#make a figure with title and labeled axes
p = figure(title='Line Plot using Bokeh',x_axis_label='X Axis',
y_axis_label='Y Axis')
#add a line to your canvas
p.line(x,y)
#create the plot and open browser
show(p)
Let’s break down the code for better understanding:
- From the module
plotting
we’re importing two functionsfigure
andshow
we’ll explain them in detail shortly. - Set some values for data points
x
andy
. - Calling the
figure()
function and storing its output in a variablep
. figure()
is a function that is used to create a blank canvas for our plots, we can specify the properties of this canvas liketitle
and labels for x and y axis usingx_axis_label
andy_axis_label
respectively.- Adding a line to that canvas using
p.line(x,y)
note that thex
andy
are the same point coordinates we’ve mentioned. - Finally, calling the
show()
function and passing the figurep
. The show function creates the plot for the above-mentioned parameters and displays the same in a browser.
Please note, that the above code ran in command prompt using:
python myfile.py
The final output would look something like this:
Looks elegant and sophisticated, moreover if you look at the top right corner you can see the tools which come with each plot. These tools allow us to toggle area zoom, mouse wheel zoom, save the plot locally, navigate the plot, reset these settings and much more.
Let us make a plot with multiple lines:
from bokeh.plotting import figure,show
x = [0,1,2]
u = [0,6,10]
w = [0,4,7]
y = [4,7,9]
v = [4,2,5]
z = [4,5,8]
#make a figure
p = figure(title='Multi Line Plot using Bokeh',x_axis_label='X Axis',
y_axis_label='Y Axis')
#add a line to your canvas
p.line(x,y,line_width=2,color='green')
#add another line to your canvas
p.line(u,v,line_width=2,color='red')
#add a third line to your canvas
p.line(w,z,line_width=2,color='blue')
#create the plot and open browser
show(p)
And the resultant plot is:
Works perfectly, now add as many lines as you wish and call the show function to get them displayed.
But what if, instead of lines we want to plot circles?
Bokeh has you covered there as well, let’s plot a scatter plot, which is essentially nothing but a line plot without the lines and data points marked as circles, we’ll make use of the below code:
from bokeh.plotting import figure,show
import numpy as np
x = np.linspace(0,50,15) #15 equidistant points
y = np.random.rand(15) #15 random values
#make a figure
p = figure(title='Scatter Plot using Bokeh',x_axis_label='X Axis',
y_axis_label='Y Axis')
#add circles to your canvas
p.circle(x,y,color='green',size=15)
#create the plot and open browser
show(p)
The output comes out to be:
Note the x
and y
changes, those I’ve made using NumPy’s functions to generate dummy data. If you’d like to know more about NumPy visit here, I’ve also created a zero-cost cheat sheet to get you started on NumPy in no time, access that here.
In terms of Bokeh, the only change we’ve made is this line:
p.circle(x,y,color='green',size=15)
We’ve replaced the function line
with circle
and the only additional argument we’ve passed is the size
which helps us set the dimensions of the circles being plotted. Isn’t this convenient?
Just like multi-line plots between the figure
and show
functions you can add as many lines and circles as you’d like, there are of course many more plotting functions to be utilized like vertical and horizontal bar charts vbar
and hbar
, stacked bar charts like vbar_stacked
, hbar_stacked
etc. You can find all available chart types and their required parameters in the Bokeh documentation. I would also highly recommend going through the user guide to get a hang of things first.
Bonus Tip: Plotting Bokeh plots in stand-alone Jupyter Notebooks
Jupyter (and similar other) notebooks are essential when it comes to building a Data Science project. They let us run the code in isolated blocks and of course are capable of displaying the output right on the spot. It makes sense for us to know how to run our Bokeh plots natively in such notebooks. Here, I’m taking the example of Jupyter but you can try the same with other notebooks as well and let me know.
In a separate cell just before our plotting code add the following lines:
from bokeh.io import output_notebook
output_notebook()
That’s it, you should see a message saying “BokehJS successfully loaded”, this will do the trick, and every time you call the show
function it will load the plot in the notebook itself.
Conclusion:
Bokeh is a powerful library which can be used to display beautiful visualization in the browser, it has a syntax which feels familiar to people with prior experience and is equally easy to learn for new beginners as well.
I hope that you enjoyed reading this article, it would mean THE WORLD to me if you could show your appreciation through applause, share and following the author. It helps motivating me to bring valuable content to you.
P.S. You can click on the clap button as many times as you like, it’ll keep increasing the count!
Follow me on Github and LinkedIn.
See you next time and Thanks for reading!