悬停工具提示不适用于Networkx Graph的Bokeh可视化

时间:2019-01-04 14:58:34

标签: pandas graph networkx bokeh

我正在尝试使用Networkx制作图形并使用Bokeh渲染图形。除了我已经查阅过Data tooltips in Bokeh don't show data, showing '???' insteadBokeh hover tooltip not displaying all data - Ipython notebook并阅读了ColumnDataSource上的文档的悬停工具提示之外,其他所有内容似乎都正常运行,但是我仍然无法弄清楚为什么工具提示不会从其中一个呈现标签数据框的列数。

下面是我一直在使用的excel文件中数据的过度简化。

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

我尝试运行以下代码:

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)

运行脚本后,我得到以下信息:

screenshit 组列标签正确显示,但子主题未正确显示。我将不胜感激任何想法或指示。

1 个答案:

答案 0 :(得分:2)

您正在创建ColumnDataSource,但实际上并未将其用于任何用途。 CDS仅仅由于已创建就没有效果,它必须配置为驱动字形或表等。悬停工具使用字形已配置的数据源,在这种情况下,该数据源由{{1 }}。您可以访问节点渲染器的数据源,以向其添加任何其他列(例如,用于悬停工具):

from_networkx