首页 > 解决方案 > Holoviews 热图动画标签滑块

问题描述

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

代码作为动画成功运行,但会产生一个单帧如下所示的输出: 在此处输入图像描述

我有两个问题:

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'], 100),
                          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')


标签: python-3.xanimationbokehheatmapholoviews

解决方案


推荐阅读