networkx - 使用 Holoviews 的 from_networkx 方法时如何将节点属性传递给 HoverTool 工具提示
问题描述
全息视图、散景和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)
俱乐部”:“先生。嗨'},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'}})
G_all.nodes(data=True)
NodeDataView({12240216: {'crew_name': u'a'}, ... 8421367: {'crew_name': u'b'}})
抱歉,如果我根本没有把问题说清楚,我会很感激我能得到的任何帮助,因为我已经连续几个小时在这个问题上猛烈抨击了!
谢谢 :)
解决方案
您可以尝试将inspection_policy 设置为“nodes”
或者,您可以将其添加到/从属性到边缘,然后使用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'
)
推荐阅读
- go - Go 会改变单个 goroutine 中两个互斥锁的执行顺序吗?
- python - 如何通过 python 将多页数据导出到 Excel/CSV?
- html - 使用 DOM 元素作为掩码
- design-patterns - 观察者模式设计和 RXJS 库
- bunifu - 表单边框样式未正确显示
- scala - java.lang.NoClassDefFoundError: org/vafer/jdeb/Console 启动 intellij sbt 项目时
- html - 当具有不同的高度时,如何将具有 flex 布局的 div 彼此向下定位?
- jenkins - jenkins管道中输入值为null时如何设置字符串输入默认值
- python - 使用 zipfile 解压缩档案会将 exe 转换为文本文件 (MacOS)
- javascript - ChartsJS Annotations Plugin - 你能创建一个工具提示来伴随注释吗?