问题描述
我试图用不同的时间来注释一些时间序列数据。为此,我创建了一个曲线叠加层和一个 holoviews.element.raster.Image(参见下面的代码)。
根据 here 和 here,我知道轴应该具有相同的单位和标签,以便全息视图自动链接它们。我已经在下面的示例代码中尝试过这个,但这些图仍然没有链接。
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'),[pd.Timestamp('2017-10-01'),color='red')
line3 = hv.Curve([
[pd.Timestamp('2017-11-01'),[pd.Timestamp('2017-12-01'),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”(但为了显示而重新标记为“日期”),另一个是“日期”:
要使它们相同,您不仅需要使它们标记相同,还需要声明为相同的维度,例如:
(如何在屏幕上标记尺寸取决于您;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'),[pd.Timestamp('2017-10-01'),color='red')
line3 = hv.Curve([
[pd.Timestamp('2017-11-01'),[pd.Timestamp('2017-12-01'),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