Python / Bokeh - 具有日期时间格式的Colorbar

时间:2018-04-24 17:59:57

标签: python bokeh colorbar

有人可以给我一点帮助,将散景条上的自动收报机格式化为日期时间吗?

下面的示例使用日期时间索引为散点图着色。我希望将colorbar格式化为显示年/月的几个刻度,类似于figure(x_axis_type ='datetime')的工作方式。

Plot。目前它以毫秒显示时间。这可能与在LinearColorMapper()中设置低和高参数的正确值,然后从DatetimeTickFormatter()中获取正确的格式有关

玩具示例:

import pandas as pd
import numpy as np
import bokeh.plotting as bk_plt
import bokeh.palettes as bk_pal
import bokeh.models as bk_mod

bk_plt.output_notebook()

Data = pd.DataFrame(index = pd.date_range(start = '2012-04-01', end = '2013-08-16', freq = 'H'))
Data['X'] = np.random.rand(len(Data.index))
Data['Y'] = np.random.rand(len(Data.index))

Data['Time'] = Data.index.to_julian_date()
CMin = Data['Time'].min()
CRange = Data['Time'].max() - Data['Time'].min()
Cols = (Data['Time'] - CMin) * 255 // CRange
Data['Colors'] = np.array(bk_pal.Plasma256)[Cols.astype(int).tolist()]

color_mapper = bk_mod.LinearColorMapper(palette='Plasma256', low = int(Data.index[0].strftime("%s")) * 1000, high = int(Data.index[-1].strftime("%s")) * 1000)

color_bar = bk_mod.ColorBar(color_mapper=color_mapper, ticker=bk_mod.BasicTicker(), formatter = bk_mod.DatetimeTickFormatter(), label_standoff=12, border_line_color=None, location=(0,0))


p = bk_plt.figure()
p.circle(x = Data.X, y = Data.Y, size = 5, color = Data.Colors, alpha = 0.5)
p.add_layout(color_bar, 'right')
bk_plt.show(p)

1 个答案:

答案 0 :(得分:1)

正如bigreddot指出的那样,自从纪元以来,散景日期时间表示为浮点数。 LinearColorMapper需要低和高定义如下:

color_mapper = bk_mod.LinearColorMapper(
    palette='Plasma256', 
    low = int(Data.index[0].strftime("%s")) * 1000, 
    high = int(Data.index[-1].strftime("%s")) * 1000
)