gpt4 book ai didi

python - 将 Hover 从列表添加到 Bokeh 热图

转载 作者:行者123 更新时间:2023-11-28 17:33:19 25 4
gpt4 key购买 nike

我现在使用 bokeh 几天了,我用默认参数创建了这个热图:

bokeh-1

x 轴是星期几,因此每个个案代表一天。我想将日期添加到悬停,但我很难做到,因为它不在源表上。

我的表是这样的:

enter image description here

我创建了一个列表,其中包含每个案例的每个日期,但是当我调用时:

dates = table.index.tolist()

hover = HoverTool(
tooltips=[
("date", "@dates"),
]
)

hm = HeatMap(table, title="heatmap freq", width=800, palette=palette,
tools=[hover, PanTool(), PreviewSaveTool(), ResetTool()])
show(hm)

我收到以下 ValueError :

---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-32-59002c86b85c> in <module>()
29 )
30
---> 31 hm = HeatMap(table, title="heatmap freq", width=800, palette=palette, tools=[hover, PanTool(), PreviewSaveTool(), ResetTool()])
32 show(hm)

C:\Anaconda3\lib\site-packages\bokeh\charts\builder\heatmap_builder.py in HeatMap(values, xscale, yscale, xgrid, ygrid, **kw)
65 chart = create_and_build(
66 HeatMapBuilder, values, xscale=xscale, yscale=yscale,
---> 67 xgrid=xgrid, ygrid=ygrid, **kw
68 )
69 chart.add_tools(HoverTool(tooltips=[("value", "@rate")]))

C:\Anaconda3\lib\site-packages\bokeh\charts\_builder.py in create_and_build(builder_class, values, **kws)
39 # create a chart to return, since there isn't one already
40 chart_kws = { k:v for k,v in kws.items() if k not in builder_props}
---> 41 chart = Chart(**chart_kws)
42 chart.add_builder(builder)
43

C:\Anaconda3\lib\site-packages\bokeh\charts\_chart.py in __init__(self, *args, **kwargs)
55
56 def __init__(self, *args, **kwargs):
---> 57 self._options = opts_type(**kwargs)
58 orig_init(self)
59

C:\Anaconda3\lib\site-packages\bokeh\properties.py in __init__(self, **properties)
355
356 for name, value in properties.items():
--> 357 setattr(self, name, value)
358
359 def __setattr__(self, name, value):

C:\Anaconda3\lib\site-packages\bokeh\properties.py in __setattr__(self, name, value)
361
362 if name.startswith("_") or name in props:
--> 363 super(HasProps, self).__setattr__(name, value)
364 else:
365 matches, text = difflib.get_close_matches(name.lower(), props), "similar"

C:\Anaconda3\lib\site-packages\bokeh\properties.py in __set__(self, obj, value)
210 break
211 else:
--> 212 raise e
213 else:
214 value = self.transform(value)

C:\Anaconda3\lib\site-packages\bokeh\properties.py in __set__(self, obj, value)
203 def __set__(self, obj, value):
204 try:
--> 205 self.validate(value)
206 except ValueError as e:
207 for tp, converter in self.alternatives:

C:\Anaconda3\lib\site-packages\bokeh\properties.py in validate(self, value)
836
837 if not (value is None or any(param.is_valid(value) for param in self.type_params)):
--> 838 raise ValueError("expected an element of either %s, got %r" % (nice_join(self.type_params), value))
839
840 def transform(self, value):

ValueError: expected an element of either Bool or String, got [<bokeh.models.tools.HoverTool object at 0x000000001CCC0748>, <bokeh.models.tools.PanTool object at 0x000000000FAFB7B8>, <bokeh.models.tools.PreviewSaveTool object at 0x000000002717ADD8>, <bokeh.models.tools.ResetTool object at 0x000000002717A6D8>]

我认为问题出在我的 dates 列表不在 table 上。我尝试将日期放在 ColumnDataSource 中,但是我不知道如何在 HeatMap 函数中调用它。

有什么想法吗?

编辑:使用@mdurant 回答。

最佳答案

通过为 tools= 使用字符串形式,您正在使用默认的悬停工具实例。要实际使用您在代码中创建的实例,您需要将该实例显式包含在实例列表中:

hover = HoverTool(
tooltips=[
("date", "@dates"),
]
)
hm = HeatMap(table, title="heatmap freq", width=800, palette=palette,
tools=[hover, PanTool(), PreviewSaveTool(), ResetTool()])
show(p)

(这些类在 bokeh.models 中定义)

关于python - 将 Hover 从列表添加到 Bokeh 热图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32762191/

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