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.