In [17]:
from bokeh.plotting import *
from bokeh.objects import ColumnDataSource

from pprint import pprint

output_notebook()


Bokeh Plot

Configuring embedded BokehJS mode.


In [18]:
colormap = [
    "#444444", "#a6cee3", "#1f78b4", "#b2df8a", "#33a02c",
    "#fb9a99", "#e31a1c", "#fdbf6f", "#ff7f00", "#cab2d6"
]

These next three lists will contain an x-index, a y-index, and an associated color for each of the 10x10 grid cells.

(This is a fairly naïve algorithm to generate the lists, but it should make the format of the data more apparent.)


In [19]:
x_inds = []
y_inds = []
colors = []

for i in range(10):
    for j in range(10):
        x_inds.append(i)
        y_inds.append(j)
        if i == j:
            colors.append(colormap[i])
        else:
            colors.append('lightgrey')

Let's take a peek.


In [20]:
pprint(zip(zip(x_inds, y_inds), colors)[:23])


[((0, 0), '#444444'),
 ((0, 1), 'lightgrey'),
 ((0, 2), 'lightgrey'),
 ((0, 3), 'lightgrey'),
 ((0, 4), 'lightgrey'),
 ((0, 5), 'lightgrey'),
 ((0, 6), 'lightgrey'),
 ((0, 7), 'lightgrey'),
 ((0, 8), 'lightgrey'),
 ((0, 9), 'lightgrey'),
 ((1, 0), 'lightgrey'),
 ((1, 1), '#a6cee3'),
 ((1, 2), 'lightgrey'),
 ((1, 3), 'lightgrey'),
 ((1, 4), 'lightgrey'),
 ((1, 5), 'lightgrey'),
 ((1, 6), 'lightgrey'),
 ((1, 7), 'lightgrey'),
 ((1, 8), 'lightgrey'),
 ((1, 9), 'lightgrey'),
 ((2, 0), 'lightgrey'),
 ((2, 1), 'lightgrey'),
 ((2, 2), '#1f78b4')]

The categorical axes accept lists of strings, like so: ['1', '2', '3', ..., '9'].

The corresponding x and y indices are cast to strings before they are passed to ColumnDataSource.


In [21]:
categories = [str(x) for x in range(10)]

source = ColumnDataSource(
    data = dict(
        x = [str(x) for x in x_inds],
        y = [str(y) for y in y_inds],
        color = colors,
    )
)

Our call to rect() will pass the categories to x_range and y_range, and specify the corresponding attribute names of ColumnDataSource.


In [22]:
rect('x', 'y',             # use the 'x' and 'y' fields to target each cell
     0.9, 0.9,             # use 0.9 for both width and height of each rectangle 
     color = 'color',      # use the 'color' field to set the color
     source = source,      # use the data source we created above
     x_range = categories, # sequence of categorical coords for x-axis
     y_range = categories, # sequence of categorical coords for y-axis
)

show()


Bokeh Plot
Plots

Tadaa!!