悬停工具在散景中不起作用

Hover tool not working in Bokeh

我有一个 table,其中包含学生访问 activity 的次数。

  df_act5236920.head()

    activities  studs
 0  3.0       student 1
 1  4.0       student 10
 2  5.0       student 11
 3  6.0       student 12
 4  2.0       student 13
 5  4.0       student 14
 6  19.0      student 15

如果我尝试通过以下代码将悬停工具添加到此数据框创建的条形图中:

 from bokeh.charts import Bar
 from bokeh.models import Legend

 from collections import OrderedDict
 TOOLS = "pan,wheel_zoom,box_zoom,reset,hover,save"
 bar = Bar(df_act5236920,values='activities',label='studs',title = "Activity 5236920 performed by students",
      xlabel="Students",ylabel="Activity",legend=False,tools=TOOLS)
 hover = bar.select_one(HoverTool)
 hover.point_policy = "follow_mouse"
 hover.tooltips = OrderedDict([
     ("Student Name", "@studs"),
     ("Access Count", "@activities"),
 ])
 show(bar)

当我将鼠标悬停在条形图上时,它显示学生值而不是活动值,我什至尝试使用“$activities”,但结果仍然相同。

根据我阅读的其他堆栈溢出问题,我尝试使用 ColumnDataSource 而不是 DataFrame,如下面的代码所示:

source = ColumnDataSource(ColumnDataSource.from_df(df_act5236920))

from collections import OrderedDict
TOOLS = "pan,wheel_zoom,box_zoom,reset,hover,save"
bar = Bar('studs','activities',source=source, title = "Activity 5236920 performed by students",tools=TOOLS)
hover = bar.select_one(HoverTool)
hover.point_policy = "follow_mouse"
hover.tooltips = OrderedDict([
    ("Student Name", "@studs"),
    ("Access Count", "@activities"),
])
show(bar)

它给我以下错误:

    ---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-76-81505464c390> in <module>()
  3 # bar = Bar(df_act5236920,values='activities',label='studs',title = "Activity 5236920 performed by students",
  4 # xlabel="Students",ylabel="Activity",legend=False,tools=TOOLS)
  ----> 5 bar = Bar('studs','activities',source=source, title = "Activity 5236920 performed by students",tools=TOOLS)
  6 hover = bar.select_one(HoverTool)
  7 hover.point_policy = "follow_mouse"

C:\Anaconda2\lib\site-packages\bokeh\charts\builders\bar_builder.pyc in  Bar(data, label, values, color, stack, group, agg, xscale, yscale, xgrid, ygrid, continuous_range, **kw)
319     kw['y_range'] = y_range
320 
--> 321     chart = create_and_build(BarBuilder, data, **kw)
322 
323     # hide x labels if there is a single value, implying stacking only

C:\Anaconda2\lib\site-packages\bokeh\charts\builder.pyc in create_and_build(builder_class, *data, **kws)
 66     # create the new builder
 67     builder_kws = {k: v for k, v in kws.items() if k in builder_props}
---> 68     builder = builder_class(*data, **builder_kws)
 69 
 70     # create a chart to return, since there isn't one already

C:\Anaconda2\lib\site-packages\bokeh\charts\builder.pyc in __init__(self, *args, **kws)
292             # make sure that the builder dimensions have access to the chart data source
293             for dim in self.dimensions:
--> 294                 getattr(getattr(self, dim), 'set_data')(data)
295 
296             # handle input attrs and ensure attrs have access to data

C:\Anaconda2\lib\site-packages\bokeh\charts\properties.pyc in set_data(self, data)
170             data (`ChartDataSource`): the data source associated with the chart
171         """
--> 172         self.selection = data[self.name]
173         self._chart_source = data
174         self._data = data.df

TypeError: 'NoneType' object has no attribute '__getitem__'

我什至尝试通过以值列表的形式将数据帧的列传递给它来从头开始创建 ColumnDataSource,但我仍然遇到与上面显示的错误相同的错误

source = ColumnDataSource(data=dict(
     studs=students,
     activities=activity_5236920,
))

当我尝试在热图上实施 hovertool 时,我也遇到了同样的问题。谁能帮忙解决这个问题?

所以,在查阅了大量文档之后,我终于弄明白了一些事情。

首先,NoneType 错误是由于对于条形图,您需要传递数据框和 ColumnDataSource,才能显示条形图。 所以代码需要是:

bar = Bar(df_act5236920,values='activities',label='studs',title = "Activity 5236920 performed by students",
      xlabel="Students",ylabel="Activity",legend=False,tools=TOOLS,source=source)

注意 Bar() 方法中如何同时提及数据框名称和 source=source。 对于未显示值的第二个问题,我使用了@height,它实际上显示了所选栏的高度,在本例中是计数值。

hover.tooltips = OrderedDict([
   ("Student Name", "@studs"),
   ("Access Count", "@height"),
]) 

对于学生姓名值,@x 和@studs 都有效。但是我仍然无法解决的唯一问题是,尽管我已经提到了 ColumnDataSource "source",但它并没有真正为我完成任何事情,因为当我尝试在 hover.tooltips 中使用@activities 时,它仍然给我一个“???”的回应。所以,我不确定这是怎么回事。这是我在尝试构建的另一个时间序列可视化中遇到的问题。