Unfortunately Bokeh 0.4.4 broke nbviewer rendering capabilities, but this has been fixed on master and will be working again with the release of 0.5. In the meantime, it's worth double-checking to make sure you're on a working version.
import bokeh
bokeh.print_versions()
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= Bokeh version: 0.4.4-267-ge9807af Python version: 2.7.6 |Anaconda 1.9.1 (x86_64)| (default, Jan 10 2014, 11:23:15) [GCC 4.0.1 (Apple Inc. build 5493)] Platform: Darwin-13.1.0-x86_64 (Darwin Kernel Version 13.1.0: Wed Apr 2 23:52:02 PDT 2014; root:xnu-2422.92.1~2/RELEASE_X86_64) -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
from bokeh.plotting import *
from bokeh.objects import HoverTool
from collections import OrderedDict
#5 datasets (rows)
#8 buckets (columns)
buckets = ['AMOUNT', 'BUYER', 'TENDER TRACKING', 'TENDER FEATURES', 'AWARD TRACKING', 'AWARD FEATURES', 'SYSTEM', 'SUPPLIER']
datasets = ['Canada', 'Turkey', 'France', 'UK', 'Moldova']
data = [
('Canada', [1, 2, 1, 2, 1, 2, 1, 2]),
('Turkey', [2, 2, 2, 2, 0, 0, 1, 1]),
('France', [3, 2, 1, 3, 2, 1, 3, 2]),
('UK', [1, 1, 1, 0, 0, 0, 0, 1]),
('Moldova', [2, 3, 4, 1, 1, 2, 6, 9]),
]
x = []
y = []
size = []
alpha = []
for i in xrange(len(datasets)):
for j in xrange(len(buckets)):
dataset = datasets[i]
bucket = buckets[j]
x.append(dataset)
y.append(bucket)
size.append(0.8) # Fixed size
alpha.append(data[i][1][j]*0.1) # <-- alpha as a "intensity" measure.
line_alpha = [0.1 if a == 0 else a for a in alpha] # <-- to get borders over 0 "intensity" squares
This ColumnDataSource
object will also serve as the source for our hover tooltips. You can see an extended example in the Bokeh documentation.
source = ColumnDataSource(
data=dict(
x=x,
y=y,
size=size,
alpha=alpha, # <-- passed alpha to the source to be showed (later) by the hover tool
)
)
Flatten data for labels!
labels = [tup[1] for tup in data]
labels = [str(item) for sublist in labels for item in sublist]
# draw it
figure()
hold() # <-- so we can add our new text renderer
visual_properties = {
'color': 'red', # <-- another color to try
'fill_alpha': alpha, # <-- alpha "defined" by our data
'line_color': 'red', # <-- add borders to the squares
'line_alpha': line_alpha, # <-- borders with the same alpha except for the 0 "intensity" square (cosmetic)
'plot_height': 1000,
'plot_width': 800,
'title': None,
}
rect('x', 'y', 'size', 'size',# <-- Passing "size" as width and height parameters from our `source`
source=source,
x_range=datasets,
y_range=buckets,
tools="hover", # <-- Added "hover" tool
marker="square",# <-- No it's not pretty, but there are (just now discovered) issues with
# hit-detection for the circle glyph on categorical axes.
**visual_properties)
text(x, y,
text=labels, # <-- labels!
text_color="black",
angle=0)
grid().grid_line_color = None
This is how we currently get ahold of our hover tools. Yes it's a little clunky (for now), but at least it's a one-liner! :)
hover = [t for t in curplot().tools if isinstance(t, HoverTool)][0]
hover.tooltips = OrderedDict([
("Dataset", "@x"),
("Bucket", "@y"),
("Alpha", "@alpha"),
])
Then we make user of the OrderedDict
to declare the tooltip contents.
show()