首页 > 解决方案 > 如何在 Jupyter Notebook 中动态更新 Holoviews 绘图布局?

问题描述

我想通过一个情节的项目在 Jupyter Notebook 中工作。用新数据或新布局更新它,也许在它附近添加/删除其他绘图等等……在 Bokeh 中,我可以使用类似于:

 target = show(layout, notebook_handle=True)
 push_notebook(handle=target) 

在 Holoviews 中,我发现了如何将新数据提供给现有绘图:

pipe = Pipe(data=[])
Image = hv.DynamicMap(hv.Image, streams=[pipe1])
pipe.send(np.random.rand(3,2)) #data change

但是有任何解决方案来管理 Holoviews 中的实时布局更新吗?是否可以通过 .opts() 构造更新现有图?在这个例子中,我将得到一个新的情节:

pipe = Pipe(data=[])
Image = hv.DynamicMap(hv.Image, streams=[pipe])
Image.opts(width=1000,height=1000)
#######new cell in jupyter notebook############
Image.opts(width=100,height=100)

标签: jupyter-notebookbokehholoviews

解决方案


这是我对我的问题的一个绝妙答案:

import param
import panel as pn
import numpy as np
import holoviews as hv
from holoviews.streams import Pipe
pn.extension()
pipe = Pipe(data=[])

class Layout(param.Parameterized):
    colormap = param.ObjectSelector(default='viridis', objects=['viridis', 'fire'])
    width = param.Integer(default=500)
    update_data = param.Action(lambda x: x.param.trigger('update_data'), label='Update data!')
    
    @param.depends("update_data", watch=True)
    def _update_data(self):
        pipe.send(np.random.rand(3,2))

layout = Layout()
Image = hv.DynamicMap(hv.Image, streams=[pipe]).apply.opts(cmap=layout.param.colormap, width=layout.width)
pdmap = pn.panel(Image)
playout = pn.panel(layout)
def update_width(*events):
    for event in events:
        if event.what == "value":
            pdmap.width = event.new
layout.param.watch(update_width, parameter_names=['width'], onlychanged=False)

pn.Row(playout, pdmap)

https://discourse.holoviz.org/t/how-to-update-holoviews-plots-width-height/1947


推荐阅读