简体   繁体   中英

Can't plot heatmap in Bokeh with datetime x axis

I'm trying to plot the following simple heatmap:

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)

Unfortunately it doesn't render properly:

在此输入图像描述

I've tried setting x_range but nothing seems to work.

I've managed to get something working with the following code:

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,
)

However as soon as I try to use the zoom tool, I get the following errors in console:

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

I've using Bokeh 0.12.3.

The bokeh.charts , including HeatMap was deprecated and removed in 2017. You should use the stable and supported bokeh.plotting API. With your data above, a complete example:

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)

在此输入图像描述

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