gpt4 book ai didi

python - 变量变化时的 Panel/Hvplot 交互

转载 作者:太空宇宙 更新时间:2023-11-03 15:36:01 25 4
gpt4 key购买 nike

我正在尝试创建一个包含两个全息 View 对象的仪表板:一个 panel pn.widgets.Select 包含 xarray 变量列表和 hvplot 的对象在输入时采用选定变量的对象,如下所示:

def hvmesh(var=None):
mesh = ds[var].hvplot.quadmesh(x='x', y='y', rasterize=True, crs=crs,
width=600, height=400, groupby=list(ds[var].dims[:-2]), cmap='jet')
return mesh

下面是特定变量(具有时间和高度维度的变量)的示例网格: enter image description here

当我从面板小部件中选择一个变量时,我想更新 map : enter image description here我尝试将其作为动态 map 来执行,如下所示:

from holoviews.streams import Params
import holoviews as hv

var_stream = Params(var_select, ['value'], rename={'value': 'var'})

mesh = hv.DynamicMap(hvmesh, streams=[var_stream])

但是当我尝试显示 map 时,我得到:

Exception: Nesting a DynamicMap inside a DynamicMap is not supported.

从面板小部件中为 hvplot 选择变量似乎很常见。使用 pyviz 完成此操作的最佳方法是什么? ?

如果它有用,这是我的 full attempt Jupyter Notebook .

最佳答案

因为 groupby 随着每个变量的选择而变化,变量列表不能传递给 hvplot。因此,一种解决方案是每次选择新变量时重新创建绘图。这有效:

import holoviews as hv
from holoviews.streams import Params

def plot(var=None, tiles=None):
var = var or var_select.value
tiles = tiles or map_select.value
mesh = ds[var].hvplot.quadmesh(x='x', y='y', rasterize=True, crs=crs, title=var,
width=600, height=400, groupby=list(ds[var].dims[:-2]),
cmap='jet')
return mesh.opts(alpha=0.7) * tiles

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

def on_map_select(event):
tiles = event.obj.value
col[-1] = plot(tiles=tiles)

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

col = pn.Column(var_select, map_select, plot(var_select.value) * tiles)

制作: enter image description here

这是 full notebook .

关于python - 变量变化时的 Panel/Hvplot 交互,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54769115/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com