If you've ever wondered how blogs and news websites create interactive data visualisations, chances are they either use d3.js (written in javascript), or Bokeh (written in Python). If you're looking to spend some time to make your visualisations stand out from the rest, Bokeh is a great way to do it. By the end of this guide you should have some idea how to create interactive visualisations and be able to start your journey to make world class data visualisations.
Bokeh can output to HTML or directly into the notebook, but we need to tell it which one we want. For outputting to our notebook we just use the output_notebook() function:
import numpy as np
import pandas as pd
import bokeh.plotting as bkp
bkp.output_notebook()
It's worth noting that although Bokeh code is written in Python, it works via Javascript behind the scenes, however we don't really need to worry about this.
Plotting a scatter graph is similar to plotting using matplotlib - we take a list or array of x and y points in and pass them as a function. The difference here is that we create a figure then call a method on it to plot:
x = np.linspace(-10,10,101)
y = np.sin(x)
fig = bkp.figure(width=500,height=200)
fig.circle(x,y,size=7,color="red", alpha=0.8)
bkp.show(fig)
The are a number of shapes we can use to plot, more can be found in the documentation here.
Let's also look at another helpful plot - the line plot. This works in exactly the same way to the scatter plot, but we use the line method instead of the circle method.
fig2 = bkp.figure(width=500,height=200)
fig2.line(x,y, line_width=2)
bkp.show(fig2)
Plotting shapes on our axis is simple - we just use the method for the shape we want. For rectangles this is quad, and takes arguements for the top, bottom, left and right of each rectangle:
fig3 = bkp.figure(width=500,height=500)
fig3.quad(top=[5,3,5], bottom=[0,2,0], left=[0,1,3], right=[1,3,4], color="#ff9933")
bkp.show(fig3)
We could use this method for bar graphs, but thankfully Bokeh gives us the vbar and hbar methods to speed things up:
fig4 = bkp.figure(width=500,height=500)
fig4.vbar(x=[1,2,3], width=0.3, bottom=0, top=[1,2,3])
bkp.show(fig4)
There are many, many more methods for plotting in Bokeh, but for the sake of space, we're not going to look at them here. Next up - annotating our plots.
Bokeh gives us a number of ways to annotate our plots. The simplest being title, which we include in the figure function:
fig = bkp.figure(title="My First Plot", width=500,height=200)
fig.circle(x,y,size=7,color="red", alpha=0.8)
bkp.show(fig)
Similarly to matplotlib, there is more than one way to edit the title of a plot. We can edit the attributes of a plot after initialising it by assigning values to methods on the plot:
fig5 = bkp.figure(width=500,height=200)
fig5.circle([1,2],[1,2], size=100, alpha=0.6)
fig5.title.text = "I'm a new title!"
fig5.title.align = "right"
fig5.title.text_font_size = "30px"
bkp.show(fig5)
Here we can see that the toolbar is unhelpfully placed on the right of the graph, meaning it hangs awkwardly off the side of our plot. We can change the location of the toolbar with the toolbar_location arguement or method:
fig5.toolbar_location="above"
fig5.title.align = "left"
bkp.show(fig5)
Finally, adding a legend can take some work, but is simple enough - we can either plot the two sets seperately or provide a list of labels corresponding with our data points.
x = np.linspace(-10,10,101)
y1 = np.sin(x)
y2 = np.cos(x)
fig6 = bkp.figure(height=200)
fig6.circle(x,y1,color='blue',legend='y1',alpha=0.5,size=6)
fig6.circle(x,y2,color='red', legend='y2', alpha=0.5,size=6)
bkp.show(fig6)
Your challenge is to create the HiPy logo as best you can using Bokeh shapes! If you're finding this hard - try starting with rectangles and then look up the patch plotting method that allows custom shapes. From here it should be pretty easy. Good Luck!