python-3.x - Holoviews 热图动画标签滑块
问题描述
我使用带有“散景”渲染器的全息视图创建了一个动画热图。代码如下并在 Jupyter Notebook 中运行。基本上我编写了一个热图字典,然后我使用“hv.DynamicMap”和“hv.streams”来创建动画,即。dict 的流键并渲染关联的热图。
我有两个问题:
我希望滑块显示字典的“键”,而不仅仅是像现在那样显示键列表的索引。所以在上图中的“a0”不是“0”。我怎样才能做到这一点?
有关于“固定宽度”的警告。我已经设置了所有对象的高度和宽度,为什么会有警告?
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')
解决方案
推荐阅读
- sql - Comparing 2 tables and updating a column based on results?
- python - how to remove points in an array around NaN values within a window
- deployment - Serverless: Service files not changed. Skipping deployment
- r - 使用条件删除行
- java - 为什么物理后退按钮与应用程序后退按钮的工作方式不同
- c# - C# - 在 try 块之外定义 var 并将其返回
- python - how to correctly read text file in pandas?
- python - 如何使用python比较两个html文件并仅显示新html文件中的差异
- javascript - 动态地从一组属性和值中删除数组/对象
- sql - SQL code for algebraic formula