For details on bokeh, see http://bokeh.pydata.org/en/latest/docs/user_guide.html#userguide
In [18]:
from bokeh.plotting import figure, output_file, show, output_notebook, vplot
import random
import numpy as np
import pandas as pd
output_notebook() # Use so see output in the Jupyter notebook
In [20]:
import bokeh
bokeh.__version__
Out[20]:
Steps
In [2]:
from IPython.display import Image
Image(filename='biological_data.png')
Out[2]:
In [3]:
df_bio = pd.read_csv("biological_data.csv")
df_bio.head()
Out[3]:
Desired visualization
In [22]:
plot = figure(plot_width=400, plot_height=400)
plot.circle(df_bio['rate'], df_bio['yield'])
plot.xaxis.axis_label = 'rate'
plot.yaxis.axis_label = 'yield'
show(plot)
Let's distinguish the lines with colors. First, how many lines are there?
In [5]:
# What are the possible colors
df_bio['line'].unique()
Out[5]:
In [6]:
# Generate a plot with a different color for each line
colors = {'HA': 'red', 'HR': 'green', 'UA': 'blue', 'WT': 'purple'}
plot = figure(plot_width=700, plot_height=800)
plot.title.text = 'Phenotypes for evolutionary lines.'
for line in list(colors.keys()):
df = df_bio[df_bio.line == line]
color = colors[line]
plot.circle(df['rate'], df['yield'], color=color, legend=line)
plot.legend.location = "top_right"
show(plot)
What colors are possible to use? Check out bokeh.palettes
In [7]:
import bokeh.palettes as palettes
print palettes.__doc__
#palettes.magma(4)
Exercise: Handle colors for the plot for an arbitrary number of evolutionary lines. (Hint: construct the colors dictionary using the values of 'line' and a palette.)
In [8]:
# Generate the colors dictionary
# Fill this in....
In [9]:
# Plot with the generated palette
# Fill this in ...
Tools can be specified and positioned when the Figure is created. The interaction workflow is (a) select a tool (identified by vertical blue line), (b) perform gesture for tool.
In [23]:
TOOLS = 'box_zoom,box_select,resize,reset'
plot = figure(plot_width=200, plot_height=200, title=None, tools=TOOLS)
plot.scatter(range(10), range(10))
show(plot)
In [24]:
from bokeh.models import HoverTool, BoxSelectTool
TOOLS = [HoverTool(), BoxSelectTool()]
plot = figure(plot_width=200, plot_height=200, title=None, tools=TOOLS)
show(plot)
Figure
Glyph
Tool
Based on our knowledge of Bokeh concepts, is a Tool associated with Figure or Glyph?
Which classes will be involved in hovering:
Start with some examples. First, simple hovering.
In [25]:
from bokeh.plotting import figure, output_file, show
from bokeh.models import HoverTool, BoxSelectTool
output_file("toolbar.html")
TOOLS = [BoxSelectTool(), HoverTool()]
p = figure(plot_width=400, plot_height=400, title=None, tools=TOOLS)
p.circle([1, 2, 3, 4, 5], [2, 5, 8, 2, 7], size=10)
show(p)
Now add ad-hoc data
In [30]:
from bokeh.plotting import figure, output_file, show, ColumnDataSource
from bokeh.models import HoverTool
output_file("toolbar.html")
hover = HoverTool(
tooltips=[
("index", "$index"),
("(x,y)", "(@x, @y)"),
("desc", "@desc"),
]
)
p = figure(plot_width=400, plot_height=400, tools=[hover],
title="Mouse over the dots")
source = ColumnDataSource(
data={
'x': [1, 2, 3, 4, 5],
'y': [2, 5, 8, 2, 7],
'desc': ['A', 'b', 'C', 'd', 'E'],
}
)
p.circle('x', 'y', size=20, source=source)
show(p)
Exercise: Plot the biological data with colors and a hover that shows the evolutionary line.
See widget.py and my_app.py
In [16]:
Image(filename='BokehArchitecture.png')
Out[16]: