Analytics Vidhya
Published in

Analytics Vidhya

Beginners Guide to Data Visualization with Bokeh

Building web-based visualization in Python from scratch

First things first

pip install bokeh

Importing necessary packages.

from bokeh.plotting import figure
from bokeh.io import output_file, show
from bokeh.sampledata.iris import flowers
from bokeh.models import HoverTool, ColumnDataSource

Defining the output file path

output_file(’iris.html’)’’'Plotting and customizing code'''
show()

Creating a figure object

output_file('iris.html')
f = figure()
'''
Plotting and customizing code'''
show(f)

Let’s plot

output_file("iris1.html")f=figure()f.circle(x=flowers['petal_length'], y=flowers['petal_width'])show(f)

Bokeh tools

Adding background-color

f.plot_width=1100
f.plot_height=650
f.background_fill_color='olive'
f.background_fill_alpha=0.3

Adding a title and axis labels

f.title.text='Iris Morphology'
f.title.text_color='Olive'
f.title.text_font='times'
f.title.text_font_size='25px'
f.title.align='center'
f.xaxis.axis_label='Petal Length'
f.yaxis.axis_label='Petal Width'
f.axis.axis_label_text_color='blue'
f.axis.minor_tick_line_color='blue'
f.yaxis.major_label_orientation='vertical'
f.axis.major_label_text_color='orange'

ColumnDataSource

setosa = ColumnDataSource(flowers[flowers["species"]=="setosa"])versicolor = ColumnDataSource(flowers[flowers["species"]=="versicolor"])virginica = ColumnDataSource(flowers[flowers["species"]=="virginica"])

Adding color and legend

f.legend.location = 'top_left'
f.legend.label_text_color = 'olive'
f.legend.label_text_font = 'times'
f.legend.border_line_color = 'black'
f.legend.margin = 10
f.legend.padding = 18

Adding hover effects

HTML code to add species image

<div>
<img
src="@imgs" height="42" alt="@imgs" width="42"
style="float: left; margin: 0px 15px 15px 0px;"
border="2">
</img>
</div>
#image url to species
urlmap = {'setosa':'https://upload.wikimedia.org/wikipedia/commons/thumb/5/56/Kosaciec_szczecinkowaty_Iris_setosa.jpg/800px-Kosaciec_szczecinkowaty_Iris_setosa.jpg',
'versicolor':'https://upload.wikimedia.org/wikipedia/commons/thumb/2/27/Blue_Flag%2C_Ottawa.jpg/800px-Blue_Flag%2C_Ottawa.jpg',
'virginica':'https://upload.wikimedia.org/wikipedia/commons/thumb/9/9f/Iris_virginica.jpg/800px-Iris_virginica.jpg'}
#creating new img column in DataFrame with image url as values
flowers['imgs'] = [urlmap[x] for x in flowers['species']]

--

--

Analytics Vidhya is a community of Analytics and Data Science professionals. We are building the next-gen data science ecosystem https://www.analyticsvidhya.com

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store