无法使用日期时间x轴在Bokeh中绘制热图

时间:2016-10-25 15:06:25

标签: python bokeh

我正在尝试绘制以下简单的热图:

data = {
    'value': [1, 2, 3, 4, 5, 6],
    'x': [datetime(2016, 10, 25, 0, 0),
          datetime(2016, 10, 25, 8, 0),
          datetime(2016, 10, 25, 16, 0),
          datetime(2016, 10, 25, 0, 0),
          datetime(2016, 10, 25, 8, 0),
          datetime(2016, 10, 25, 16, 0)],
    'y': ['param1', 'param1', 'param1', 'param2', 'param2', 'param2']
}
hm = HeatMap(data, x='x', y='y', values='value', stat=None)
output_file('heatmap.html')
show(hm)

不幸的是它没有正确呈现:

enter image description here

我尝试过设置x_range但似乎没什么用。

我设法使用以下代码:

d1 = data['x'][0]
d2 = data['x'][-1]

p = figure(
    x_axis_type="datetime", x_range=(d1, d2), y_range=data['y'],
    tools='xpan, xwheel_zoom, reset, save, resize,'
)

p.rect(
    source=ColumnDataSource(data), x='x', y='y', width=12000000, height=1,
)

但是,只要我尝试使用缩放工具,我就会在控制台中收到以下错误:

Uncaught Error: Number property 'start' given invalid value: 
Uncaught TypeError: Cannot read property 'indexOf' of null

我使用的是Bokeh 0.12.3。

1 个答案:

答案 0 :(得分:0)

2017年已弃用并删除了bokeh.charts,包括HeatMap。您应该使用稳定且受支持的bokeh.plotting API。根据您的数据,一个完整的例子:

from datetime import datetime

from bokeh.plotting import figure, show
from bokeh.transform import linear_cmap

data = {
    'value': [1, 2, 3, 4, 5, 6],
    'x': [datetime(2016, 10, 25, 0, 0),
          datetime(2016, 10, 25, 8, 0),
          datetime(2016, 10, 25, 16, 0),
          datetime(2016, 10, 25, 0, 0),
          datetime(2016, 10, 25, 8, 0),
          datetime(2016, 10, 25, 16, 0)],
    'y': ['param1', 'param1', 'param1', 'param2', 'param2', 'param2']
}

p = figure(x_axis_type='datetime', y_range=('param1', 'param2'))

EIGHT_HOURS = 8*60*60*1000

p.rect(x='x', y='y', width=EIGHT_HOURS, height=1, line_color="white",
       fill_color=linear_cmap('value', 'Spectral6', 1, 6), source=data)

show(p)

enter image description here

相关问题