1

我有一个 4D 数据集,并且hvplot可以很好地为我在以下文件中指定的尺寸生成小部件groupby

import xarray as xr

ds = xr.open_dataset('http://gamone.whoi.edu/thredds/dodsC/coawst_4/use/fmrc/coawst_4_use_best.ncd')

rho_vars = ['salt','temp']

from cartopy import crs as ccrs
import hvplot.xarray
import holoviews as hv
from geoviews import tile_sources as gvts
import panel as pn

var_select = pn.widgets.Select(name='Variables:', options=rho_vars, 
                               value='temp')

crs = ccrs.PlateCarree()

def plot(var=None):
    base_map = gvts.OSM
    var = var or var_select.value
    mesh = ds[var][-24:,:,:].hvplot.quadmesh(x='lon_rho', y='lat_rho', rasterize=True, title=var,
                                    width=600, height=400, crs=crs,
                                    groupby=list(ds[var].dims[:-2]), cmap='jet')
    overlay = (base_map * mesh.opts(alpha=0.7)).opts(active_tools=['wheel_zoom', 'pan'])
    widgets = {dim: pn.widgets.Select for dim in ds[var].dims[:-2]}
    return pn.holoviews.HoloViews(overlay, widgets=widgets).layout

def on_var_select(event):
    var = event.obj.value
    dashboard[-1] = plot(var=var)

var_select.param.watch(on_var_select, parameter_names=['value']);

dashboard = pn.Column(var_select, plot(var_select.value))

产生:

在此处输入图像描述

但是,虽然hvplot默认为 的第一个值,但options OrderedDict我想默认为最后一个值。

如果我查看仪表板元素:

print(dashboard)

Column
    [0] Select(name='Variables:', options=OrderedDict([('salt', ...]), value='temp')
    [1] Row
        [0] HoloViews(DynamicMap, widgets={'time': <class 'panel.wid...})
        [1] Column
            [0] Select(name='Forecast time f..., options=OrderedDict([('2019-03-09 ...]), value=numpy.datetime64('2019-03-...)
            [1] Select(name='S-coordinate a..., options=OrderedDict([('-0.96875', ...]), value=-0.96875)

我看到我可以指定Select小部件值事后像:

dashboard[1][1][1].value = next(reversed(dashboard[1][1][1].options.values())) 

这确实有效:

在此处输入图像描述

但是,我想制作一个独立的仪表板,当用户选择一个新变量时,它默认为最后一个值。With the current code, when a new variable is selected, the default value is unfortunately again set to the first value.

有没有办法将我的事后代码嵌入到我的绘图函数中,或者是否有另一种更好的方法来实现这一点?

完整的笔记本在这里

4

2 回答 2

1

如果我正确理解了您的问题,则 hvplot的“字段”参数可以做到这一点。

于 2020-05-15T17:07:46.633 回答
0

正如您所看到的,当print(dashboard)您拥有小部件的默认值时。



Column
    [0] Select(name='Variables:', options=OrderedDict([('salt', ...]), value='temp')
    [1] Row
        [0] HoloViews(DynamicMap, widgets={'time': <class 'panel.wid...})
        [1] Column
            [0] Select(name='Forecast time f..., options=OrderedDict([('2019-03-09 ...]), value=numpy.datetime64('2019-03-...)
            [1] Select(name='S-coordinate a..., options=OrderedDict([('-0.96875', ...]), value=-0.96875)

这里,默认值为-0.96875第二个小部件。

我会在这条线上工作widgets = {dim: pn.widgets.Select for dim in ds[var].dims[:-2]}以放置默认值。

default_value = [...,...]
widgets = {dim: pn.widgets.Select(value=default_val[dim]) for dim in ds[var].dims[:-2]}
于 2019-03-12T12:32:06.717 回答