简体   繁体   中英

Python 3 Bokeh Heatmap Rect simple example not showing anything in plot

I'm trying to color code a simple categorical heatmap using python's Bokeh library. For example, given the following table, I would want to replace each 'A' with a red square and each 'B' with a blue square:

AAAABAAAAB
BBBAAAABBB

To start, I thought the following would produce 2 rows of 10 squares of the same color. but I just get a blank plot. I must be missing a core concept of how to create categorical heatmaps in bokeh. To start, I was trying to mimic an example on the bokeh site:

https://docs.bokeh.org/en/latest/docs/gallery/categorical.html

Does anyone see what I'm missing? (This is a simple example. I have many rows with hundreds of columns that I need to color by category.)

from bokeh.plotting import figure, show, output_file

hm = figure()
colors = ['#2765a3' for x in range(20)]
x_input = [x for x in range(10)]
y_input = ['a', 'b']
hm.rect(x_input, y_input, width = 1, height = 1, color = colors)
output_file('test.html)
show(hm)

You need to create specific coordinates for each rect . If there are 2 possible values on the y-axis, and 10 possible values on the x-axis, then there are 20 possible unique pairs of coordinates for all the rects (ie the cross product of those two lists). For example:

(0, 'A'), (0, 'B'), (1, 'A'), (1, 'B'), ...

If you split each of these tuples into an x-coordinate and y-coordinate, and collect x's and y's into their own lists, they you can see why there must be both 20 x-coordinates and 20 y-coordinates.

Additionally, for categorical coordinates, you have to tell figure what they are. Here is your code updated:

from bokeh.plotting import figure, show

colors = ['#2765a3' for x in range(20)]
x = list(range(10)) * 2
y = ['a'] * 10 +  ['b'] * 10

hm = figure(y_range=('a', 'b'))
hm.rect(x, y, width=1, height=1, fill_color=colors, line_color="white")

show(hm)

在此处输入图片说明

The User's Guide section on Categorical Data has much more information on how to use categoricals in Bokeh, including complete examples of heat maps

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM