使用回调更改 DynamicMap 的平移

Change pan of DynamicMap with callback

我有时间序列数据,我想在 bokeh 服务器中使用 holoviews 每天显示这些数据。我的代码归结为:

import pandas as pd
import numpy as np
import holoviews as hv

hv.extension("bokeh", "matplotlib")
renderer = hv.renderer('bokeh')

df = pd.DataFrame(pd.date_range("2019-11-01", "2019-11-07", freq="H"), columns=["timestamp"])
df["level"] = 17
df = df.set_index("timestamp")
ds = hv.Dataset(df, kdims="timestamp", vdims="level")

days = list(sorted({t.date() for t in df.index}))
pattern_dim = hv.Dimension('Day', values=days)

dmap = hv.DynamicMap(lambda d: ds[d:d + np.timedelta64(1, 'D')].to(hv.Curve), kdims=[pattern_dim])
doc = renderer.server_doc(dmap)

但是,当我使用 bokeh serve ... 中的滑块更改日期时,我必须手动调整平移以查看新数据。这可以使用回调来完成吗?

没有 holoviewsbokeh 有类似的要求:Manually change x range for Bokeh plot

解决方法是使用DataRange1d

import pandas as pd
import numpy as np
import holoviews as hv
from bokeh.models import DataRange1d
from bokeh.plotting import Figure

hv.extension("bokeh", "matplotlib")
renderer = hv.renderer('bokeh')

df = pd.DataFrame(pd.date_range("2019-11-01", "2019-11-07", freq="H"), columns=["timestamp"])
df["level"] = 17
df = df.set_index("timestamp")
ds = hv.Dataset(df, kdims="timestamp", vdims="level")

days = list(sorted({t.date() for t in df.index}))
pattern_dim = hv.Dimension('Day', values=days)
dmap = hv.DynamicMap(lambda d: ds[d:d + np.timedelta64(1, 'D')].to(hv.Curve), kdims=[pattern_dim])
doc = renderer.server_doc(dmap)
for x in doc.select({'type': Figure}):
    x.x_range = DataRange1d()

在这个 github 问题中发现:https://github.com/pyviz/holoviews/issues/2441