将Hover从列表添加到散热图

时间:2015-09-24 13:08:07

标签: python plot heatmap bokeh

我现在使用散景几天,然后用默认参数创建了这个HeatMap:

bokeh-1

xaxis是周年,因此每个案例代表一天。我想将日期添加到悬停,但我无法做到这一点,因为它不在源表上。

我的表格如下:

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回答。

1 个答案:

答案 0 :(得分:2)

通过使用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中定义)