# Cannot compose with xarray.Dataset with different dimensions

**URL:** <https://discourse.holoviz.org/t/cannot-compose-with-xarray-dataset-with-different-dimensions/6978>\
**Category:** hvPlot\
**Tags:** xarray\
**Created:** [March 16, 2024, 3:09pm UTC](https://discourse.holoviz.org/t/cannot-compose-with-xarray-dataset-with-different-dimensions/6978 "2024-03-16T15:09:31Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![mothersup](https://discourse.holoviz.org/user_avatar/discourse.holoviz.org/mothersup/32/6405_2.png) [@mothersup](https://discourse.holoviz.org/u/mothersup)\
**Post date:** [March 16, 2024, 3:09pm UTC](https://discourse.holoviz.org/t/cannot-compose-with-xarray-dataset-with-different-dimensions/6978/1 "2024-03-16T15:09:31Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![ahuang11](https://discourse.holoviz.org/user_avatar/discourse.holoviz.org/ahuang11/32/4947_2.png) [@ahuang11](https://discourse.holoviz.org/u/ahuang11)\
**Post date:** [March 17, 2024, 3:33am UTC](https://discourse.holoviz.org/t/cannot-compose-with-xarray-dataset-with-different-dimensions/6978/2 "2024-03-17T03:33:05Z")

</div>

Do you need interactive here? Would this work?

```auto
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

```

---

<div class="post-metadata">

**Author:** ![mothersup](https://discourse.holoviz.org/user_avatar/discourse.holoviz.org/mothersup/32/6405_2.png) [@mothersup](https://discourse.holoviz.org/u/mothersup)\
**Post date:** [March 21, 2024, 10:21am UTC](https://discourse.holoviz.org/t/cannot-compose-with-xarray-dataset-with-different-dimensions/6978/3 "2024-03-21T10:21:33Z")

</div>

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`.

---

<div class="post-metadata">

**Author:** ![ahuang11](https://discourse.holoviz.org/user_avatar/discourse.holoviz.org/ahuang11/32/4947_2.png) [@ahuang11](https://discourse.holoviz.org/u/ahuang11)\
**Post date:** [March 21, 2024, 3:15pm UTC](https://discourse.holoviz.org/t/cannot-compose-with-xarray-dataset-with-different-dimensions/6978/4 "2024-03-21T15:15:35Z")

</div>

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

```auto
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

```

---

<div class="post-metadata">

**Author:** ![Marc](https://discourse.holoviz.org/user_avatar/discourse.holoviz.org/marc/32/24_2.png) [@Marc](https://discourse.holoviz.org/u/Marc)\
**Post date:** [March 23, 2024, 4:51am UTC](https://discourse.holoviz.org/t/cannot-compose-with-xarray-dataset-with-different-dimensions/6978/5 "2024-03-23T04:51:03Z")

</div>

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

- [Reactive Functions and Expressions - Param](https://param.holoviz.org/user_guide/Reactive_Expressions.html)
- [ReactiveExpr- Panel](https://panel.holoviz.org/reference/panes/ReactiveExpr.html)

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