3

I have a table that contains the number of times a student accessed an 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

If I try to add the hover tool to the bar chart created by this dataframe through the code below:

 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)

When I hover over the bar chart, it shows the student value but not the activities values, I even tried using "$activities" but the result is still the same.

enter image description here

I tried using ColumnDataSource instead of DataFrame based on other stack overflow questions I read, as is apparent in the code below:

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)

It gives me the following error:

    ---------------------------------------------------------------------------
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__'

I even tried creating the ColumnDataSource from scratch by passing the columns of the dataframe to it in the form of a list of values, but I still got the same error as the one shown above

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

I'm having the same issue when I try to implement the hovertool on a heatmap as well. Can anyone help in how to fix this?

4
  • Try source = ColumnDataSource(df_act5236920) from_df() was deprecated in Bokeh 0.9.3. Might be worth a shot Sep 30, 2016 at 3:46
  • @BobHaffner I tried it, but the error is still the same. The source object was created with the other two implementations I mentioned as well. I'm assuming that maybe they're empty and that's why it's throwing a NoneType error, but i don't know why it's empty.
    – Sift
    Sep 30, 2016 at 10:42
  • Ok. Regarding the error, I think your source is fine. It's just that Bar() expects a data source as the first arg and you're passing a string Try bar = Bar(df_act5236920,label='studs', values='activities',source=source, title = "Activity 5236920 performed by students",tools=TOOLS). No clue on your tool tip problem tho. Best of luck Sep 30, 2016 at 13:56
  • @BobHaffnerjust saw your answer, I was parallely adding my own answer as well with the same resolution and have partially resolved the tooltip issue as well. Thanks for your help.
    – Sift
    Sep 30, 2016 at 14:06

1 Answer 1

0

So, after going through a lot of the documentation, I've finally figured out a few things.

Firstly, the NoneType error was due to the fact that for a bar chart, you need to pass the dataframe as well as the ColumnDataSource, for the bar-chart to display. So the code needed to be:

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

Notice how the dataframe name and source=source are both mentioned in the Bar() method. For the second issue of the value not being displayed, I used @height which essentially displayed the height of the selected bar, which in this case was the count value.

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

For the student name value, @x and @studs both work. But the only thing I still couldn't resolve was that although I have mentioned the ColumnDataSource "source", it didn't really accomplish anything for me because when I try to use @activities in hover.tooltips, it still gives me a response of "???". So, I'm not sure what that is all about. And it is an issue that I'm sturggling with in another Time Series visualisation that I'm trying to build.

Not the answer you're looking for? Browse other questions tagged or ask your own question.