Photo: https://unsplash.com/@thesollers

Python Data Visualization with Bokeh

Samet Girgin
PursuitOfData

--

Bokeh is an interactive visualization library and is used mainly in streaming datasets. It can be helpful to create interactive plots, dashboards and data applications.

Firstly it is required to install bokeh to use in the python environment, → pipenv install bokeh is written in the terminal and waited to be installed. After that, it will be ready to import to our script. Let’s start with a basic example to plot x vs y plot:

from bokeh.plotting import figure, output_file, showx= [1,2,3,4]
y=[4,6,2,4,3]
output_file("index.html")#Add plot
p= figure(title="Simple Example", x_axis_label="X Axis", y_axis_label="Y Axis")
#Render glyph
p.line(x,y, legend="Test", line_width=2)
show(p)

Here is a very small dataset that includes car, horsepower, price, and image links.

from bokeh.plotting import figure, output_file, show
import pandas as pd
#Read csv file
dataset= pd.read_csv("cars.csv")
car=dataset["Car"]
horsepower= dataset["Horsepower"]
output_file("index.html")#Add plot
p= figure(y_range=car, plot_width=800, plot_height=600, title="Car with Top HP", x_axis_label="Horsepower",
tools="pan,box_select,zoom_in,zoom_out,save,reset")
#Render glyph
p.hbar(y=car, right=horsepower,left=0,height=0.4,color="orange",fill_alpha=0.5 )
show(p)

The link below contains documentation related to different types of bar charts with Bokeh:

https://docs.bokeh.org/en/latest/docs/reference/models/glyphs/hbar.html

Up to now, we return the car vs horsepower chart but we also have image and price columns. Here is how we create a more interactive graph by using Bokeh.

from bokeh.plotting import figure, output_file, show,save, ColumnDataSource
from bokeh.models.tools import HoverTool
import pandas as pd
#Read csv file
dataset= pd.read_csv("cars.csv")
#Create ColumnDataSource from data frame
source = ColumnDataSource(dataset)
output_file("index.html")#Car List
car_list = source.data["Car"].tolist()
#Add plot
p= figure(y_range=car_list, plot_width=800, plot_height=600, title="Car with Top HP", x_axis_label="Horsepower",
tools="pan,box_select,zoom_in,zoom_out,save,reset")
#Render glyph
p.hbar(y="Car", right="Horsepower",left=0,height=0.4,color="orange",fill_alpha=0.5, source=source )
#Ad Tooltips:
hover =HoverTool()
hover.tooltips ="""
<div>
<h3>@Car</h3>
<div><strong>Price: </strong>@Price</div>
<div><strong>HP: </strong>@Horsepower</div>
<div><img src="@Image" alt="" width="200" /></div>
</div>
"""
p.add_tools(hover)
#Show results
show(p)
#Save file
save(p)

Color Frames/Color Mapper: Let’s create a color map with legend

from bokeh.plotting import figure, output_file, show,save, ColumnDataSource
from bokeh.models.tools import HoverTool
from bokeh.transform import factor_cmap
from bokeh.palettes import Blues8
import pandas as pd
#Read csv file
dataset= pd.read_csv("cars.csv")
#Create ColumnDataSource from data frame
source = ColumnDataSource(dataset)
output_file("index.html")#Car List
car_list = source.data["Car"].tolist()
#Add plot
p= figure(y_range=car_list, plot_width=800, plot_height=600, title="Car with Top HP", x_axis_label="Horsepower",
tools="pan,box_select,zoom_in,zoom_out,save,reset")
#Render glyph/ For cmap use fill_color instead of color
p.hbar(y="Car", right="Horsepower",left=0,height=0.4,fill_color=factor_cmap("Car", palette=Blues8, factors=car_list),
fill_alpha=0.9, source=source, legend="Car" )
#Add Legend
p.legend.orientation="vertical"
p.legend.location="top_right"
p.legend.label_text_font_size="10px"
#Ad Tooltips:
hover =HoverTool()
hover.tooltips ="""
<div>
<h3>@Car</h3>
<div><strong>Price: </strong>@Price</div>
<div><strong>HP: </strong>@Horsepower</div>
<div><img src="@Image" alt="" width="200" /></div>
</div>
"""
p.add_tools(hover)
#Show results
show(p)
#Save file
save(p)

The last thing is how it is possible to generate a div and a script tag if we want to put this into another file.

from bokeh.plotting import figure, output_file, show,save, ColumnDataSource
from bokeh.models.tools import HoverTool
from bokeh.transform import factor_cmap
from bokeh.palettes import Blues8
from bokeh.embed import components
import pandas as pd
#Read csv file
dataset= pd.read_csv("cars.csv")
#Create ColumnDataSource from data frame
source = ColumnDataSource(dataset)
output_file("index.html")#Car List
car_list = source.data["Car"].tolist()
#Add plot
p= figure(y_range=car_list, plot_width=800, plot_height=600, title="Car with Top HP", x_axis_label="Horsepower",
tools="pan,box_select,zoom_in,zoom_out,save,reset")
#Render glyph/ For cmap use fill_color instead of color
p.hbar(y="Car", right="Horsepower",left=0,height=0.4,fill_color=factor_cmap("Car", palette=Blues8, factors=car_list),
fill_alpha=0.9, source=source, legend="Car" )
#Add Legend
p.legend.orientation="vertical"
p.legend.location="top_right"
p.legend.label_text_font_size="10px"
#Ad Tooltips:
hover =HoverTool()
hover.tooltips ="""
<div>
<h3>@Car</h3>
<div><strong>Price: </strong>@Price</div>
<div><strong>HP: </strong>@Horsepower</div>
<div><img src="@Image" alt="" width="200" /></div>
</div>
"""
p.add_tools(hover)
#Show results
#show(p)
#Save file
save(p)
#Print out div and script
script, div = components(p)
print(div)
print(script)

References and Further Readings:

https://towardsdatascience.com/data-visualization-with-bokeh-in-python-part-one-getting-started-a11655a467d4

Bokeh Documentation: https://docs.bokeh.org/en/latest/docs/reference/models/glyphs/hbar.html

https://github.com/bradtraversy/python_bokeh_chart/blob/master/main.py

Python Data Visualization With Bokeh: https://www.youtube.com/watch?v=2TR_6VaVSOs

https://docs.bokeh.org/en/latest/docs/reference/models/glyphs/hbar.html

--

--

Samet Girgin
PursuitOfData

Data Analyst, Petroleum & Natural Gas Engineer, PMP®