Python: networkx 自定义图形节点标签?
Python: networkx customize graph node labels?
我有以下示例代码:
import pandas as pd
import networkx as nx
import matplotlib.pyplot as plt
def view_graph(graph, labels):
'''
Plots the graph
'''
pos = nx.spring_layout(graph)
nx.draw(graph, pos, with_labels=True)
nx.draw_networkx_edge_labels(graph, pos,edge_labels=labels)
plt.axis('off')
plt.show()
index_to_name = {1: "Paul", 2: "Magda", 3: "Paul", 4: "Anna", 5: "Marie", 6: "John", 7: "Mark"}
relation = {}
g = nx.DiGraph()
g.add_edge(1, 4)
relation[(1,4)] = "dad"
g.add_edge(2, 4)
relation[(2,4)] = "mom"
g.add_edge(1, 5)
relation[(1,5)] = "dad"
g.add_edge(2, 5)
relation[(2,5)] = "mom"
g.add_edge(3, 6)
relation[(1,6)] = "dad"
g.add_edge(4, 6)
relation[(2,6)] = "mom"
g.add_edge(3, 7)
relation[(1,7)] = "dad"
g.add_edge(4, 7)
relation[(2,7)] = "mom"
view_graph(g, relation)
我的插图window看起来像这样:
我有两个问题,不知道如何解决:
- 他们是否有可能在插图中显示字典
index_to_name
中的名字而不是我引入的索引以确保唯一节点(在字典中名字 Paul 出现了两次,所以名字“Paul " 应该有两个节点)。
- 如何解决图中用红色矩形标记的边缘标签的问题。我的目标是添加这些标签,以便清楚参考了哪条边?
希望你能帮我解决这两个问题
W.r.t。问题 1),您可以将 with_labels
设置为 False(或简单地省略参数),然后添加对 nx.draw_networkx_labels
:
的函数调用
import matplotlib.pyplot as plt
import networkx as nx
def view_graph(graph, node_labels, edge_labels):
'''
Plots the graph
'''
pos = nx.spring_layout(graph)
nx.draw(graph, pos, node_color='lightgray')
nx.draw_networkx_labels(graph, pos, labels=node_labels)
nx.draw_networkx_edge_labels(graph, pos, edge_labels=edge_labels)
plt.axis('off')
plt.show()
index_to_name = {1: "Paul", 2: "Magda", 3: "Paul", 4: "Anna", 5: "Marie", 6: "John", 7: "Mark"}
relation = {}
relation[(1, 4)] = "dad"
relation[(2, 4)] = "mom"
relation[(1, 5)] = "dad"
relation[(2, 5)] = "mom"
relation[(3, 6)] = "dad"
relation[(4, 6)] = "mom"
relation[(3, 7)] = "dad"
relation[(4, 7)] = "mom"
g = nx.from_edgelist(relation, nx.DiGraph())
view_graph(g, index_to_name, relation)
我有以下示例代码:
import pandas as pd
import networkx as nx
import matplotlib.pyplot as plt
def view_graph(graph, labels):
'''
Plots the graph
'''
pos = nx.spring_layout(graph)
nx.draw(graph, pos, with_labels=True)
nx.draw_networkx_edge_labels(graph, pos,edge_labels=labels)
plt.axis('off')
plt.show()
index_to_name = {1: "Paul", 2: "Magda", 3: "Paul", 4: "Anna", 5: "Marie", 6: "John", 7: "Mark"}
relation = {}
g = nx.DiGraph()
g.add_edge(1, 4)
relation[(1,4)] = "dad"
g.add_edge(2, 4)
relation[(2,4)] = "mom"
g.add_edge(1, 5)
relation[(1,5)] = "dad"
g.add_edge(2, 5)
relation[(2,5)] = "mom"
g.add_edge(3, 6)
relation[(1,6)] = "dad"
g.add_edge(4, 6)
relation[(2,6)] = "mom"
g.add_edge(3, 7)
relation[(1,7)] = "dad"
g.add_edge(4, 7)
relation[(2,7)] = "mom"
view_graph(g, relation)
我的插图window看起来像这样:
我有两个问题,不知道如何解决:
- 他们是否有可能在插图中显示字典
index_to_name
中的名字而不是我引入的索引以确保唯一节点(在字典中名字 Paul 出现了两次,所以名字“Paul " 应该有两个节点)。 - 如何解决图中用红色矩形标记的边缘标签的问题。我的目标是添加这些标签,以便清楚参考了哪条边?
希望你能帮我解决这两个问题
W.r.t。问题 1),您可以将 with_labels
设置为 False(或简单地省略参数),然后添加对 nx.draw_networkx_labels
:
import matplotlib.pyplot as plt
import networkx as nx
def view_graph(graph, node_labels, edge_labels):
'''
Plots the graph
'''
pos = nx.spring_layout(graph)
nx.draw(graph, pos, node_color='lightgray')
nx.draw_networkx_labels(graph, pos, labels=node_labels)
nx.draw_networkx_edge_labels(graph, pos, edge_labels=edge_labels)
plt.axis('off')
plt.show()
index_to_name = {1: "Paul", 2: "Magda", 3: "Paul", 4: "Anna", 5: "Marie", 6: "John", 7: "Mark"}
relation = {}
relation[(1, 4)] = "dad"
relation[(2, 4)] = "mom"
relation[(1, 5)] = "dad"
relation[(2, 5)] = "mom"
relation[(3, 6)] = "dad"
relation[(4, 6)] = "mom"
relation[(3, 7)] = "dad"
relation[(4, 7)] = "mom"
g = nx.from_edgelist(relation, nx.DiGraph())
view_graph(g, index_to_name, relation)