根据 SPARQL 查询和 Pagerank 值在 draw_networkx 可视化中突出显示节点
Highlight nodes in draw_networkx visualisation according to the SPARQL query and Pagerank value
根据我上次问的问题:Applying PageRank to a topic hierarchy tree(using SPARQL query extracted from DBpedia)
因为我目前获得了 Regulated concept map 的 PageRank 值。对于“Machine_learning”这个概念,我目前的代码如下:
from SPARQLWrapper import SPARQLWrapper, N3
from rdflib import Graph, URIRef, Literal
import networkx as nx
from networkx.readwrite import json_graph
from rdflib.extras.external_graph_libs import rdflib_to_networkx_graph
from rdflib.namespace import Namespace, RDFS, FOAF
import matplotlib.pyplot as plt
#SPARQL query for Regulated SPARQL Query Strategy
sparql = SPARQLWrapper("http://dbpedia.org/sparql")
sparql.setQuery("""construct { ?child skos:broader <http://dbpedia.org/resource/Category:Machine_learning> . ?gchild skos:broader ?child }
where {
{ ?child skos:broader <http://dbpedia.org/resource/Category:Machine_learning> . ?gchild skos:broader ?child}
UNION
{ ?gchild skos:broader/skos:broader <http://dbpedia.org/resource/Category:Machine_learning> . ?gchild skos:broader ?child}
}
""")
sparql.setReturnFormat(N3)
results = sparql.query().convert()
g = Graph()
g.parse(data=results, format="n3")
#Undirected graphs will be converted to a directed graph with two directed edges for each undirected edge.
dg = rdflib_to_networkx_graph(g, False, edge_attrs=lambda s,p,o:{})
#Draw regulated concept map
nx.draw(dg)
plt.draw()
#PageRank calculation
p1 = nx.pagerank(dg, alpha=0.85)
#p1 to pr(dict to list)
pr = sorted(p1.items(), key=lambda x:x[1],reverse=True)[:10]
#print sorted ranking
for key,val in pr:
print(key,val)
有几个问题:
- 如何根据 SPARQL 查询在 draw_networkx 可视化中突出显示节点?例如,我想将此查询中的节点
{ ?child skos:broader <http://dbpedia.org/resource/Category:Machine_learning> . ?gchild skos:broader ?child}
分配为绿色,将{ ?gchild skos:broader/skos:broader <http://dbpedia.org/resource/Category:Machine_learning> . ?gchild skos:broader ?child}
分配为红色。
- 我可以根据上面计算的 PageRank 值调整节点大小并为这些节点分配另一种颜色吗?
#Draw regulated concept map
# nx.draw(dg,pos=nx.spring_layout(dg),node_color='red') # use spring layout
# edges = nx.draw_networkx_edges(dg,pos=nx.spring_layout(dg))
pos = nx.spring_layout(dg)
source_node=copy.copy(pos)
print(source_node)
source_node_list = list(source_node.keys())
# print(source_node_list[0] in nx.spring_layout(dg))
# print(source_node_list)
options = {"node_size": 25, "alpha": 0.85}
graph=nx.draw_networkx_edges(dg, pos=pos, width=1.0, alpha=0.5)
graph=nx.draw_networkx_nodes(dg, pos=pos, nodelist=[source_node_list[1]], node_color="r", **options)
graph=nx.draw_networkx_nodes(dg, pos=pos, nodelist=[source_node_list[0],], node_color="b", **options)
graph=nx.draw_networkx_nodes(dg, pos=pos, nodelist=source_node_list[2:len(source_node_list)-1], node_color="g", **options)
# nx.draw(graph)
# plt.draw()
# nx.draw_networkx_edges(
# dg,
# pos,
# edgelist=[source_node_list[1]],
# width=8,
# alpha=0.5,
# edge_color="r",
# )
# nx.draw_networkx_edges(
# dg,
# pos,
# edgelist=[source_node_list[0]],
# width=8,
# alpha=0.5,
# edge_color="b"
# )
# nx.draw_networkx(dg, pos=nx.spring_layout(dg), node_color='blue',with_labels = False)
# labels=nx.draw_networkx_labels(dg,pos=nx.spring_layout(dg))
# nodes = nx.draw_networkx_nodes(dg,pos=nx.spring_layout(dg))
# nx.draw(dg)
# plt.draw()
非常感谢您。
我认为您可以将字典传递给绘图函数的 node_color
参数。如果您构造该字典,键是 node-names,值是您想要与这些 node-names 相关联的颜色,那么您应该能够获得所需的格式。
例如如果您已经能够 运行 一些 SPARQL 来生成一个您希望为绿色的节点列表,以及另一个您希望为蓝色的节点列表,并假设您有一个 green_list
和 blue_list
这些节点名的一对列表,然后你可以像这样构造你的字典:
# create the colour specific dictionaries
blue_dict = { n : "blue" for n in blue_list }
green_dict = { n : "green" for n in green_list }
# merge them together into a combined dictionary
known_colour_d = { **blue_dict, **green_dict }
# construct the final dictionary, leaving unknown values with a colour of "orange"
node_colours_d = { n : known_colour_d.get(n, "orange") for n in dg.nodes() }
理想情况下,您可以将 node_colours_d 放入您的参数 draw-time,它应该会为您着色。根据记忆,一些 nx 版本更喜欢颜色 delivered as a list 与 node-names 具有相同的顺序 - 但我认为这应该适用于当前版本。
或者,假设 nx 想要一个列表,那么您可以将 node_colours_d
替换为 node_colours_l
以执行相同的工作,方法是替换以下内容:
node_colours_l = [ known_colour_d.get(n, "orange") for n in dg.nodes() ]
这将创建一个列表,其中包含按顺序映射到图中每个节点外观的颜色,您可以将此列表提交给绘制函数的 node_color 参数。
根据我上次问的问题:Applying PageRank to a topic hierarchy tree(using SPARQL query extracted from DBpedia)
因为我目前获得了 Regulated concept map 的 PageRank 值。对于“Machine_learning”这个概念,我目前的代码如下:
from SPARQLWrapper import SPARQLWrapper, N3
from rdflib import Graph, URIRef, Literal
import networkx as nx
from networkx.readwrite import json_graph
from rdflib.extras.external_graph_libs import rdflib_to_networkx_graph
from rdflib.namespace import Namespace, RDFS, FOAF
import matplotlib.pyplot as plt
#SPARQL query for Regulated SPARQL Query Strategy
sparql = SPARQLWrapper("http://dbpedia.org/sparql")
sparql.setQuery("""construct { ?child skos:broader <http://dbpedia.org/resource/Category:Machine_learning> . ?gchild skos:broader ?child }
where {
{ ?child skos:broader <http://dbpedia.org/resource/Category:Machine_learning> . ?gchild skos:broader ?child}
UNION
{ ?gchild skos:broader/skos:broader <http://dbpedia.org/resource/Category:Machine_learning> . ?gchild skos:broader ?child}
}
""")
sparql.setReturnFormat(N3)
results = sparql.query().convert()
g = Graph()
g.parse(data=results, format="n3")
#Undirected graphs will be converted to a directed graph with two directed edges for each undirected edge.
dg = rdflib_to_networkx_graph(g, False, edge_attrs=lambda s,p,o:{})
#Draw regulated concept map
nx.draw(dg)
plt.draw()
#PageRank calculation
p1 = nx.pagerank(dg, alpha=0.85)
#p1 to pr(dict to list)
pr = sorted(p1.items(), key=lambda x:x[1],reverse=True)[:10]
#print sorted ranking
for key,val in pr:
print(key,val)
有几个问题:
- 如何根据 SPARQL 查询在 draw_networkx 可视化中突出显示节点?例如,我想将此查询中的节点
{ ?child skos:broader <http://dbpedia.org/resource/Category:Machine_learning> . ?gchild skos:broader ?child}
分配为绿色,将{ ?gchild skos:broader/skos:broader <http://dbpedia.org/resource/Category:Machine_learning> . ?gchild skos:broader ?child}
分配为红色。 - 我可以根据上面计算的 PageRank 值调整节点大小并为这些节点分配另一种颜色吗?
#Draw regulated concept map
# nx.draw(dg,pos=nx.spring_layout(dg),node_color='red') # use spring layout
# edges = nx.draw_networkx_edges(dg,pos=nx.spring_layout(dg))
pos = nx.spring_layout(dg)
source_node=copy.copy(pos)
print(source_node)
source_node_list = list(source_node.keys())
# print(source_node_list[0] in nx.spring_layout(dg))
# print(source_node_list)
options = {"node_size": 25, "alpha": 0.85}
graph=nx.draw_networkx_edges(dg, pos=pos, width=1.0, alpha=0.5)
graph=nx.draw_networkx_nodes(dg, pos=pos, nodelist=[source_node_list[1]], node_color="r", **options)
graph=nx.draw_networkx_nodes(dg, pos=pos, nodelist=[source_node_list[0],], node_color="b", **options)
graph=nx.draw_networkx_nodes(dg, pos=pos, nodelist=source_node_list[2:len(source_node_list)-1], node_color="g", **options)
# nx.draw(graph)
# plt.draw()
# nx.draw_networkx_edges(
# dg,
# pos,
# edgelist=[source_node_list[1]],
# width=8,
# alpha=0.5,
# edge_color="r",
# )
# nx.draw_networkx_edges(
# dg,
# pos,
# edgelist=[source_node_list[0]],
# width=8,
# alpha=0.5,
# edge_color="b"
# )
# nx.draw_networkx(dg, pos=nx.spring_layout(dg), node_color='blue',with_labels = False)
# labels=nx.draw_networkx_labels(dg,pos=nx.spring_layout(dg))
# nodes = nx.draw_networkx_nodes(dg,pos=nx.spring_layout(dg))
# nx.draw(dg)
# plt.draw()
非常感谢您。
我认为您可以将字典传递给绘图函数的 node_color
参数。如果您构造该字典,键是 node-names,值是您想要与这些 node-names 相关联的颜色,那么您应该能够获得所需的格式。
例如如果您已经能够 运行 一些 SPARQL 来生成一个您希望为绿色的节点列表,以及另一个您希望为蓝色的节点列表,并假设您有一个 green_list
和 blue_list
这些节点名的一对列表,然后你可以像这样构造你的字典:
# create the colour specific dictionaries
blue_dict = { n : "blue" for n in blue_list }
green_dict = { n : "green" for n in green_list }
# merge them together into a combined dictionary
known_colour_d = { **blue_dict, **green_dict }
# construct the final dictionary, leaving unknown values with a colour of "orange"
node_colours_d = { n : known_colour_d.get(n, "orange") for n in dg.nodes() }
理想情况下,您可以将 node_colours_d 放入您的参数 draw-time,它应该会为您着色。根据记忆,一些 nx 版本更喜欢颜色 delivered as a list 与 node-names 具有相同的顺序 - 但我认为这应该适用于当前版本。
或者,假设 nx 想要一个列表,那么您可以将 node_colours_d
替换为 node_colours_l
以执行相同的工作,方法是替换以下内容:
node_colours_l = [ known_colour_d.get(n, "orange") for n in dg.nodes() ]
这将创建一个列表,其中包含按顺序映射到图中每个节点外观的颜色,您可以将此列表提交给绘制函数的 node_color 参数。