[英]Drawing graph with labels in networkx obtained from a py2neo query
I am running some data analysis with a Jupyter
notebook where I have a query with a variable length matching like this one:我正在使用
Jupyter
笔记本运行一些数据分析,其中我有一个具有可变长度匹配的查询,如下所示:
MATCH p=(s:Skill)-[:BROADER*0..3]->(s)
WHERE s.label='py2neo' or s.label='Python'
RETURN p
I would like to plot its result as a graph, using networkx
.我想使用
networkx
将其结果绘制为图形。
So far I have found two unsatisfactory solutions.到目前为止,我发现了两个不令人满意的解决方案。 Based on an notebook here , I can generate a graph using cypher magic whose result is directly understood by the
networkx
module.基于这里的笔记本,我可以使用密码魔法生成一个图形,其结果可以被
networkx
模块直接理解。
result = %cypher MATCH p=(s:Skill)-[:BROADER*0..3]->(s) WHERE s.label='py2neo' or s.label='Python' RETURN p
nx.draw(result.get_graph())
However, then I am unable to find a way to add the labels to the plot.但是,我无法找到将标签添加到图中的方法。
That solution bypasses py2neo
.该解决方案绕过
py2neo
。 With py2neo
I can put labels on a graph, as long as I don't use a variable length pattern.使用
py2neo
我可以在图形上放置标签,只要我不使用可变长度模式。
Example:例子:
query='''MATCH p=(s1:Skill)-[:BROADER]->(s2)
WHERE s1.label='py2neo' or s1.label='Python'
RETURN s1.label as child, s2.label as parent'''
df = sgraph.data(query)
And then, copying from a response here in Stackoverflow (which I will link later) I can build the graph manually然后,从 Stackoverflow 中的响应复制(稍后我将链接),我可以手动构建图形
G=nx.DiGraph()
G.add_nodes_from(list(set(list(df.iloc[:,0]) + list(df.iloc[:,1]))))
#Add edges
tuples = [tuple(x) for x in df.values]
G.add_edges_from(tuples)
G.number_of_edges()
#Perform Graph Drawing
#A star network (sort of)
nx.draw_networkx(G)
plt.show()
With this I get a graph with labels, but to get something like the variable length matching I should use multiple queries.有了这个,我得到了一个带有标签的图,但是为了得到类似可变长度匹配的东西,我应该使用多个查询。
But how can I get the best of both worlds?但是我怎样才能做到两全其美呢? I would prefer a
py2neo
solution.我更喜欢
py2neo
解决方案。 Rephrasing: How can I get py2neo
to return a graph (not a table) and then be able to pass such information to networkx
, being able to determine which, from the multiple possible labels, are the ones to be shown in the graph?改写:我怎样才能让
py2neo
返回一个图形(而不是一个表格),然后能够将这些信息传递给networkx
,能够从多个可能的标签中确定哪些是要在图中显示的?
The question at the end was how can I get a table containing all the edges out of a subgraph that matches a certain query.最后的问题是如何从与某个查询匹配的子图中获取包含所有边的表。
The Cypher
that does the trick is:可以解决问题的
Cypher
是:
MATCH (source:Skill)-[:BROADER*0..7]->(dest:Skill)
WHERE source.label_en in ['skill1','skill2']
WITH COLLECT(DISTINCT source)+COLLECT(dest) AS myNodes
UNWIND myNodes as myNode
MATCH p=(myNode)-[:BROADER]->(neighbor)
WHERE neighbor in myNodes
RETURN myNode.label_en as child ,neighbor.label_en as parent
The first two lines get the nodes belonging to said subgraph.前两行获取属于所述子图的节点。 The last five unwind it as pairs of nodes connected by a directed edge.
最后五个将其展开为由有向边连接的节点对。 The
0
in the second MATCH
allows for collecting isolated nodes that belong to the original list.第二个
MATCH
的0
允许收集属于原始列表的孤立节点。
as in 2019, with current py2neo
packages, a way that this thing would work is与 2019 年一样,使用当前的
py2neo
软件包,这件事的工作方式是
query = '''
MATCH (source:Skill)-[:BROADER*0..7]->(dest:Skill)
WHERE source.label_en in ['skill1','skill2']
WITH COLLECT(DISTINCT source)+COLLECT(dest) AS myNodes
UNWIND myNodes as myNode
MATCH p=(myNode)-[:BROADER]->(neighbor)
WHERE neighbor in myNodes
RETURN myNode.label_en as child ,neighbor.label_en as parent
'''
df = pd.DataFrame(graph.run(query).data())
G=nx.DiGraph()
G.add_nodes_from(list(set(list(df['child']) + list(df.loc['parent']))))
#Add edges
tuples = [tuple(x) for x in df.values]
G.add_edges_from(tuples)
G.number_of_edges()
#Perform Graph Drawing
#A star network (sort of)
nx.draw_networkx(G)
plt.show()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.