Holoviews 热图动画标签滑块

问题描述

我使用带有“散景”渲染器的全息视图创建了一个动画热图。代码在下面并在 Jupyter Notebook 中运行。基本上我编写了一个热图字典,然后我使用“hv.DynamicMap”和“hv.streams”来创建动画,即。 dict 的流键并渲染相关的热图。

代码作为动画成功运行,但会产生一个输出,其中单帧如下所示:

enter image description here

我有两个问题:

  • 我希望滑块显示字典的“键”,而不仅仅是 键列表的索引,就像现在一样。所以在上面的图像中'a0'不是'0'。我该怎么做?

  • 有关于“fixed_width”的警告。我已经设置了 所有对象的高度和宽度为什么会有警告?

import numpy as np
import holoviews as hv

from bokeh.io import show,curdoc,output_notebook
from bokeh.layouts import layout
from bokeh.models import Slider,Button

renderer = hv.renderer('bokeh').instance(mode='server')
output_notebook()

# generate a dict of heatmaps
heatmap_dict = {}

for i in range(10):
    heatmap = hv.HeatMap((np.random.randint(0,10,100),np.random.choice(['A','B','C','D','E'],np.random.randn(100),np.random.randn(100)),vdims=['z','z2']).sort().aggregate(function=np.mean)
    heatmap.opts(height=400,width=400)
    heatmap_dict['a' + str(i)] = heatmap

heatmap_keys = list(heatmap_dict.keys())

# Create the holoviews app again
def mapping(phase):
    key = heatmap_keys[phase]
    return heatmap_dict[key]

stream = hv.streams.Stream.define('Phase',phase=0)()


dmap = hv.DynamicMap(mapping,streams=[stream])

# Define valid function for FunctionHandler
# when deploying as script,simply attach to curdoc
def modify_doc(doc):
    # Create Holoviews plot and attach the document
    hvplot = renderer.get_plot(dmap,doc)

    # Create a slider and play buttons
    def animate_update():
        year = slider.value + 1
        if year > end:
            year = start
        slider.value = year

    def slider_update(attrname,old,new):
        # Notify the Holoviews stream of the slider update 
        stream.event(phase=new)
        
    start,end = 0,len(heatmap_keys) - 1
    slider = Slider(start=start,end=end,value=start,step=1,title="Phase",height=30,width=180)
    slider.on_change('value',slider_update)
    
    callback_id = None

    def animate():
        global callback_id
        if button.label == '► Play':
            button.label = '❚❚ Pause'
            callback_id = doc.add_periodic_callback(animate_update,50)
        else:
            button.label = '► Play'
            doc.remove_periodic_callback(callback_id)
    button = Button(label='► Play',width=60,height=30)
    button.on_click(animate)
    
    # Combine the holoviews plot and widgets in a layout
    plot = layout([
    [hvplot.state],[slider,button]],sizing_mode='fixed')
    
    doc.add_root(plot)
    return doc

# To display in the notebook
show(modify_doc,notebook_url='localhost:8888')


解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)