Default value for widgets

Hello,

Is there any way to specify a default initial value for a widget created by hvplot.quadmesh when a groupby dimension is specified? I see a stackoverflow post from a while back that gets at it. I don’t see the requested issue filed in either panels or hvplot.

Just wondered if this ever got addressed.

I think for now the best approach is to specify the fields dict in the hvPlot call, e.g. here’s an example using the rasm sample dataset, setting the default value for the time dimension:

import hvplot.xarray
import xarray as xr

rasm = xr.tutorial.load_dataset('rasm')

rasm.hvplot.quadmesh('xc', 'yc', fields={'time': {'default': rasm.time.values[10]}}, rasterize=True, project=True)
1 Like

Thanks so much, this is exactly what I needed.

I noticed ‘fields’ isn’t documented; is this a ‘use at your own risk’ sort of thing?

Thanks!

It’s pretty well supported just a gap in the documentation. Contributions welcome :slightly_smiling_face:

@philippjfr , I tried this approach with the more complex but reproducible notebook (see the local_map method) and couldn’t get it working. I’m trying to set the season to ‘fall’, the polarization to ‘vv’, and the coherence to 12, but can’t figure out what is wrong.

This is a pretty cool use case – more than 1,000,000 geotifs being read with the zarr library into xarray, without using gdal!

If you could distill that down to something using one of the xarray sample datasets, we can probably easily diagnose it. I briefly tried to run the notebook, but gave up chasing dependencies…

Hi @rsignell,

Nice to see this little app being useful! You actually have to make two changes for this to work, and not exactly where you tried. The default parameter values have to be applied to the ‘global’ map, and they’re inferred from it to define what the ‘local’ map should display.

In practice in the method global_map the line that creates the plot should be converted to:

        self.img_dm = (
            ds
            .to(gv.QuadMesh, kdims=['longitude', 'latitude'], dynamic=True)
            .redim(season={'default': 'fall'}, polarization={'default': 'vv'}, coherence={'default': 12})
            .opts(tools=['hover'])
        )

Here the call to .redim is equivalent to passing the fields parameter to hvplot.

And in local_map the expression in the else clause must be updated to reflect that now it should be based on the default dimensions value:

        if self.img_dm.last_key:
            state = {kdim.name: val for kdim, val in zip(self.img_dm.kdims, self.img_dm.last_key)}
        else:
            state = {kdim.name: kdim.default for kdim in self.img_dm.kdims}

Interestingly when you switch to a DataArray that don’t share these variables (like lsmap) the .redim call raises no error.

1 Like