全息视图布局的交互式绘图中的链接轴

Linking axis in interactive plot for holoviews layout

我正在尝试用不同的时间来注释一些时间序列数据。为此,我创建了一个曲线叠加层和一个 holoviews.element.raster.Image(参见下面的代码)。

根据 here and 我知道轴应该具有相同的单位和标签,以便全息视图自动 link 它们。我已经在下面的示例代码中尝试过了,但绘图仍然没有 linked.

import xarray as xr
import numpy as np

# creaty dummy data
t = np.linspace(-10,10,50)
dates = pd.to_datetime(np.linspace(pd.Timestamp('2017-01-01').value, pd.Timestamp('2018-01-01').value, 200))
data = np.random.rand(50,200)
xrData = xr.DataArray(data, dims=['time','dates'], coords={'time':t, 'dates':dates})

dataplot = xrData.hvplot(width=1000)
# create annotations
line1 = hv.Curve([
    [pd.Timestamp('2017-03-01'), 0],
    [pd.Timestamp('2017-06-01'), 0]], ('x','dates'), label='eventA').opts(line_width=20, color='red')

line2 = hv.Curve([
    [pd.Timestamp('2017-08-01'), 0],
    [pd.Timestamp('2017-10-01'), 0]], ('x','dates'), label='eventA').opts(line_width=20, color='red')

line3 = hv.Curve([
    [pd.Timestamp('2017-11-01'), 0],
    [pd.Timestamp('2017-12-01'), 0]], ('x','dates'), label='eventB').opts(line_width=20, color='blue')

annotations = line1 * line2 * line3

annotations.opts(width=1000,height=150, legend_cols=2, yaxis=None)
annotations = annotations.redim(y=hv.Dimension('y', range=(-0.25, 1)))


annotated_data = (annotations + dataplot).cols(1)

annotated_data

Here is the plot I get in jupyter

好问题!在这里您想知道为什么 x 轴没有链接在这两个图之间,即使它们都被标记为“日期”并且因此 似乎 是相同的维度。但是如果您检查绘图的实际尺寸,您会发现它们是不同的;一个是“x”(但重新标记为“dates”以供显示),另一个是“dates”:

要使它们相同,您不仅需要使它们标记相同,而且要声明为相同维度,例如:

(如何在屏幕上标记尺寸取决于您;HoloViews 只关心您是否实际将尺寸表示为相同。)

这是您的代码,使用正确的导入和简化的 y 重新排列以及声明要共享的维度进行了更新:

import xarray as xr, pandas as pd, numpy as np, holoviews as hv, hvplot.xarray

# creaty dummy data
t = np.linspace(-10,10,50)
dates = pd.to_datetime(np.linspace(pd.Timestamp('2017-01-01').value, pd.Timestamp('2018-01-01').value, 200))
data = np.random.rand(50,200)
xrData = xr.DataArray(data, dims=['time','dates'], coords={'time':t, 'dates':dates})

dataplot = xrData.hvplot(width=1000)
# create annotations
line1 = hv.Curve([
    [pd.Timestamp('2017-03-01'), 0],
    [pd.Timestamp('2017-06-01'), 0]], 'dates', label='eventA').opts(line_width=20, color='red')

line2 = hv.Curve([
    [pd.Timestamp('2017-08-01'), 0],
    [pd.Timestamp('2017-10-01'), 0]], 'dates', label='eventA').opts(line_width=20, color='red')

line3 = hv.Curve([
    [pd.Timestamp('2017-11-01'), 0],
    [pd.Timestamp('2017-12-01'), 0]], 'dates', label='eventB').opts(line_width=20, color='blue')

annotations = line1 * line2 * line3

annotations.opts(width=1000,height=150, legend_cols=2, yaxis=None)
annotations = annotations.redim.range(y=(-0.25, 1))


annotated_data = (annotations + dataplot).cols(1)

annotated_data