在Bokeh图中按日期时间筛选



我可以在Bokeh中对日期时间列进行筛选。

以下代码/绘图运行良好:

# imports
import pandas as pd
from bokeh.io import output_notebook, show
from bokeh.plotting import figure
from bokeh.models import ColumnDataSource, CDSView, GroupFilter
# dataframe and columndatasource
df = pd.DataFrame({'Date': list(pd.date_range(start='1/1/2018', end='1/03/2018')) * 3,
'Value': list(range(1, 10))})
source = ColumnDataSource(df)

绘图:

p = figure()
p.line(x='Date', y='Value', source=source)
show(p)

但是,我只想绘制日期为1/3/2018的行,而不创建新的ColumnDataSource。因此,我使用了GroupFilterCDSView

last_date = source.data['Date'].max() # select 1/3/2018
date_filter = GroupFilter(column_name='Date', group=str(last_date)) # create filter
view = CDSView(source=source, filters=[date_filter]) # create view
p = figure()
p.line(x='Date', y='Value', source=source, view=view) # use view
show(p)

但是这个图没有显示任何数据?如何在Date列上进行筛选有什么建议吗?

您遇到了这个问题:https://github.com/bokeh/bokeh/issues/7524目前,GroupFilter仅适用于字符串。

一种解决方法是创建任何其他类型的过滤器并自己过滤值。

最新更新