Cannot compose with xarray.Dataset with different dimensions

Hi, I am trying to compose two interactive plots from two xarray.DataArray from the same xarray.Dataset, which one has an extra dimension. This extra dimension corresponds to a parameter, and I am plotting to compare the original data with the predictions under different settings.

The code that I use is similar to the below (which is also a MWE)

ds1 = xr.DataArray(
	data=np.arange(20).reshape((4, 5)), dims=('x', 'y'),name='ds1'
)
ds2 = xr.DataArray(
    data=np.arange(40).reshape((4, 5, 2)),dims=('x', 'y', 'z'), name='ds2'
) 
p1 = ds1.interactive.sel(y=0).hvplot.line()
p2 = ds2.interactive.sel(y=0, z=0).hvplot.line()
p1 * p2

However running this code gives me the error below:

KeyError: “One or more dimensions in the expression ((dim(‘ds1’).xr.sel(y=0)).hvplot(kind=‘line’))*((dim(‘ds2’).xr.sel(y=0, z=0)).hvplot(kind=‘line’)) could not resolve on ‘:Dataset [x,y] (ds1)’. Ensure all dimensions referenced by the expression are present on the supplied object.”

One workaround that I found was the following:

s = xr.Dataset({ds1.name: ds1, ds2.name: ds2})
p1 = s.interactive.get('ds1').sel(y=0).hvplot.line()
p2 = s.interactive.get('ds2').sel(y=0, z=0).hvplot.line()

This correctly compose the two plots into one. I think why the last one failed is that it does the sel operation on both ds1, ds2, and failed since ds1 has one less dimension.

Unfortunately, I still need to use the first one as I have some widgets callbacks that forces me to call sel before interative.

Any idea on how to compose the two plots? Any help is appreciated.

Do you need interactive here? Would this work?

ds1 = xr.DataArray(
	data=np.arange(20).reshape((4, 5)), dims=('x', 'y'),name='ds1'
)
ds2 = xr.DataArray(
    data=np.arange(40).reshape((4, 5, 2)),dims=('x', 'y', 'z'), name='ds2'
) 
p1 = ds1.sel(y=0).hvplot.line()
p2 = ds2.sel(y=0, z=0).hvplot.line()
p1 * p2

This would work, but I really need interactivity. I am using a slider to select a variable, which I will need interactive to register to the dataarray.

I personally haven’t worked with interactive, but once you have a widget, you can transform it to a panel layout

ds1 = xr.DataArray(
	data=np.arange(20).reshape((4, 5)), dims=('x', 'y'),name='ds1'
)
p1 = ds1.sel(y=0).hvplot.line()
hv_pane = pn.pane.HoloViews(p1)
hv_pane.widget_box # you can extract this
# then bind it to other things

Please note that hvplot .interactive has been generalized to param.rx. This is really the api to use as it will work much more generally. See

When Panel 1.4.0 it will add new tutorials including tutorials about param.rx/ panel.rx.