imstuck
imstuck

Reputation: 191

Hover ToolTip Not Working for Bokeh visualization of Networkx Graph

I'm trying to make graph using Networkx and rendering the graph using Bokeh. Everything seems to be working except for the hover tooltip I've consulted Data tooltips in Bokeh don't show data, showing '???' instead, Bokeh hover tooltip not displaying all data - Ipython notebook, and read the documentation on ColumnDataSource, but i still can't figure out why tooltips won't render labels from one of the columns of my dataframe.

Below is a overly simplified of the data from an excel file, which I've been playing around with.

group    subtopic    code
fruit    grapes      110A
fruit    apple       110B
meat     pork        220A
meat     chicken     220B
meat     duck        220C
vegetable lettuce    300A
vegetable tomato     310A
vegetable asparagus  320A

I tried running the following code:

df = pd.read_excel(file1, sheetname = 'Sheet3')

reshape = []

for i, j, in df.iterrows():
    for _, k in df.iterrows():
        if (j['code'] == k['code']):
            pass
        elif j['group'] == 'nan':
            reshape.append({'code1':j['code'],
                       'code2': j['code'],
                       'group': 'None'})
        elif (j['group'] == k['group']):
            reshape.append({'code1': j['code'],
                       'code2': k['code'],
                       'group': j['group']})
        else:
            pass
df1 = pd.DataFrame(reshape)

g = nx.from_pandas_edgelist(df1, source='code1', target='code2', edge_attr = True)


source = ColumnDataSource(df)

TOOLTIPS = [("type", "@group"),("code", "@code1")]

plot = Plot(x_range = Range1d(-1.1,1.1), y_range = Range1d(-1.1,1.1))
plot.title.text = 'Bokeh Plot'

plot.add_tools(HoverTool(tooltips = TOOLTIPS), TapTool(), BoxSelectTool(), 
 WheelZoomTool())

graph_renderer = from_networkx(g, nx.spring_layout, scale = 1, center = (0,0))

# manipulating nodes
graph_renderer.node_renderer.glyph = Circle(size = 15, fill_color = Spectral4[0])
graph_renderer.node_renderer.selection_glyph = Circle(size = 15, fill_color = 
 Spectral4[2])
graph_renderer.node_renderer.hover_glyph = Circle(size = 15, fill_color = 
 Spectral4[1])

# manipulating edges
graph_renderer.edge_renderer.glyph = MultiLine(line_color = '#CCCCCC', line_alpha = 
 .5, line_width = 5)
graph_renderer.edge_renderer.selection_glyph = MultiLine(line_color = Spectral4[2], 
 line_width = 5)
graph_renderer.edge_renderer.hover_glyph = MultiLine(line_color = Spectral4[1], 
 line_width = 5)

graph_renderer.selection_policy = NodesAndLinkedEdges()
graph_renderer.inspection_policy = EdgesAndLinkedNodes()

plot.renderers.append(graph_renderer)

output_notebook()
show(plot)

After I run the script, i get the following:

screenshit The group column label correctly displays, but not the subtopic. I'd appreciate any ideas or pointers.

Upvotes: 2

Views: 1710

Answers (1)

bigreddot
bigreddot

Reputation: 34568

You are creating a ColumnDataSource but then not actually using it for anything. A CDS has no effect simply by virtue of having been created, it has to be configured to drive a glyph or table, etc. The hover tool uses the data source the glyph has configured, which in this case is set up by from_networkx. You can access the data source for the node renderers, to add whatever additional columns you want to it (e.g. to use for a hover tool):

graph_renderer.node_renderer.data_source.data['code1'] = # your data here

Upvotes: 3

Related Questions