我有一个 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.
有没有办法将我的事后代码嵌入到我的绘图函数中,或者是否有另一种更好的方法来实现这一点?