使用Holoviews的from_networkx方法时如何将Node属性传递给HoverTool工具提示

时间:2019-01-23 17:53:24

标签: networkx bokeh holoviews

holoviews,bokeh和networkx的新功能。

我正在尝试建立一个可视化对象,以显示悬停时的所有节点属性。我正在使用networkx构建图,并使用Holoviews的from_networkx方法导入到holoviews图类。

我已经看到了这是相关的(即,现在在使用from_networkx时导入节点属性): https://github.com/ioam/holoviews/issues/2696

但是,我正在努力在工具提示中引用节点属性。 这是使用空手道俱乐部示例的代码,它的工作原理是:

%%opts Graph [color_index='circle']
%%opts Graph (node_size=5 edge_line_width=1)
%opts Graph [width=900 height=900]

colours = ['#000000']+hv.Cycle('Category20').values

G_kcg = nx.karate_club_graph()

hover_kcg = HoverTool(tooltips=[("Club", "@club")])

plot_kcg = hv.Graph.from_networkx(G_kcg, nx.spring_layout)
plot_kcg = plot_kcg.options(cmap=colours,tools=[hover_kcg])

bundled = bundle_graph(plot_kcg)
bundled

但是,切换到我自己的数据集后,它只会返回“ ???”在工具提示中:

%%opts Graph [color_index='circle']
%%opts Graph (node_size=5 edge_line_width=1)
%opts Graph [width=900 height=900]

colours = ['#000000']+hv.Cycle('Category20').values

hover = HoverTool(tooltips=[("Name", "@crew_name")])

plot_all = hv.Graph.from_networkx(G_all, nx.spring_layout)
plot_all = plot_all.options(cmap=colours,tools=[hover])

bundled = bundle_graph(plot_all)
bundled

两个图的NodeDataView输出对我来说都具有相同的格式:

G_kcg.nodes(data=True)
  

NodeDataView({0:{'club':'Mr. Hi'},1:{''club':'Mr. Hi'},2:{''club':'Mr. Hi'},3: {'club':'Hir先生'},4:{'club':'Hir先生'},5:{'club':'Hir先生'},6:{'club':'先生先生'嗨”},7:{'club':'嗨先生'},8:{'club':'嗨先生'},9:{'club':'Officer'},10:{'club' :“'Mr。Hi'},11:{'club':'Mr. Hi'},12:{'club':'Mr. Hi'},13:{'club':'Mr. Hi'}, 14:{'club':'Officer'},15:{'club':'Officer'},16:{'club':'Mr。Hi'},17:{'club':'Mr。Hi' },18:{'club':'Officer'},19:{'club':'Hir先生'},20:{'club':'Officer'},21:{'club':'Mr。 Hi”},22:{'club':'Officer'},23:{'club':'Officer'},24:{'club':'Officer'},25:{'club':'Officer' },26:{'club':'Officer'},27:{'club':'Officer'},28:{'club':'Officer'},29:{'club':'Officer'}, 30:{'club':'Officer'},31:{'club':'Officer'},32:{'club':'Officer'},33:{'club':'Officer'}}))< / p>

G_all.nodes(data=True)
  

NodeDataView({12240216:{'crew_name':u'a'},... 8421367:{'crew_name':u'b'}})

如果我根本没有解决问题,我们深表歉意,我将不胜枚举,因为我一直在为此努力数小时!

谢谢:)

1 个答案:

答案 0 :(得分:0)

尝试:Cannot populate Hover Tooltip values in bokeh network plot

您可以尝试将Inspection_policy设置为“节点”

或者,您可以将此属性添加到属性/从属性添加到边缘,然后使用inspection_policy'edges'

这是使用边缘法的基本示例

G = nx.karate_club_graph()

nx.set_edge_attributes(G,'','from')
nx.set_edge_attributes(G,'','to')
for edge in G.edges:
    G.edges.get(edge)['from']=G.nodes.get(edge[0])['club']
    G.edges.get(edge)['to']=G.nodes.get(edge[1])['club']

hover_kcg = HoverTool(tooltips=[("From", "@from"),("To","@to")])

hv.Graph.from_networkx(G, nx.spring_layout).opts(
    tools=[hover_kcg],
    inspection_policy='edges'    #this is where you'll want to double check
)

-或-

hover_kcg = HoverTool(tooltips=[("Club", "@club")])
hv.Graph.from_networkx(G, nx.spring_layout).opts(
    tools=[hover_kcg],
    inspection_policy='nodes'    
)