用自环标记图中的边权重
Labelling Edge Weights in Graphs with Self-Loops
@zohar.kom 他对我询问如何将我自己的标签字典添加到有向图的查询的回答给了我极大的帮助
是否也可以将边属性设置为包含具有自环的加权有向图的边权重标签?例如,对于具有五个节点的简单加权有向图,我有以下内容,标记为 A、B、C、D 和 E,存储在名为标签的字典中。
# Digraph from nonsymmetric adjacency matrix.
A=npy.matrix([[2,2,7,0,0],[0,2,6,3,0],[0,0,0,2,1],[0,0,0,0,4],
[4,0,0,0,0]])
labels={0: 'A', 1: 'B', 2: 'C', 3: 'D', 4: 'E'}
G=nx.DiGraph(A)
# Set node labels to A, B, C, D, E
nx.set_node_attributes(G, {k:{'label':labels[k]} for k in
labels.keys()})
D=to_agraph(G)
# Modify node fillcolor and edge color.
D.node_attr.update(color='blue',style='filled',fillcolor='yellow')
D.edge_attr.update(color='blue',arrowsize=1,label="???")
D.layout('dot')
D.draw('Graph.eps')
有没有办法在我有的地方插入一些东西???包括标记的边缘权重,或者可能是在使用 D=to_agraph(G)?
之前在 G 上设置边缘属性的方法
这可以按如下方式完成:
- 创建原始 (networkx) 图时读取边权重。
- 为名为 'label' 的属性设置适当的值,在您的情况下它可以是重量值。
其余保持不变:
import networkx as nx
import numpy as npy
A = npy.matrix([[2, 2, 7, 0, 0], [0, 2, 6, 3, 0], [0, 0, 0, 2, 1], [0, 0, 0, 0, 4],
[4, 0, 0, 0, 0]])
labels = {0: 'A', 1: 'B', 2: 'C', 3: 'D', 4: 'E'}
G = nx.from_numpy_matrix(A, create_using=nx.DiGraph)
# Set node labels to A, B, C, D, E
nx.set_node_attributes(G, {k: {'label': labels[k]} for k in labels.keys()})
nx.set_edge_attributes(G, {(e[0], e[1]): {'label': e[2]['weight']} for e in G.edges(data=True)})
D = nx.drawing.nx_agraph.to_agraph(G)
# Modify node fillcolor and edge color.
D.node_attr.update(color='blue', style='filled', fillcolor='yellow')
D.edge_attr.update(color='blue', arrowsize=1)
pos = D.layout('dot')
D.draw('Graph.eps')
在这里创建图表是由 G = nx.from_numpy_matrix(A, create_using=nx.DiGraph)
完成的,它将保持权重(与原始实现不同)。
向边缘添加 'label' 属性是通过 nx.set_edge_attributes(G, {(e[0], e[1]): {'label': e[2]['weight']} for e in G.edges(data=True)})
完成的。
结果是:
@zohar.kom 他对我询问如何将我自己的标签字典添加到有向图的查询的回答给了我极大的帮助
是否也可以将边属性设置为包含具有自环的加权有向图的边权重标签?例如,对于具有五个节点的简单加权有向图,我有以下内容,标记为 A、B、C、D 和 E,存储在名为标签的字典中。
# Digraph from nonsymmetric adjacency matrix.
A=npy.matrix([[2,2,7,0,0],[0,2,6,3,0],[0,0,0,2,1],[0,0,0,0,4],
[4,0,0,0,0]])
labels={0: 'A', 1: 'B', 2: 'C', 3: 'D', 4: 'E'}
G=nx.DiGraph(A)
# Set node labels to A, B, C, D, E
nx.set_node_attributes(G, {k:{'label':labels[k]} for k in
labels.keys()})
D=to_agraph(G)
# Modify node fillcolor and edge color.
D.node_attr.update(color='blue',style='filled',fillcolor='yellow')
D.edge_attr.update(color='blue',arrowsize=1,label="???")
D.layout('dot')
D.draw('Graph.eps')
有没有办法在我有的地方插入一些东西???包括标记的边缘权重,或者可能是在使用 D=to_agraph(G)?
之前在 G 上设置边缘属性的方法这可以按如下方式完成:
- 创建原始 (networkx) 图时读取边权重。
- 为名为 'label' 的属性设置适当的值,在您的情况下它可以是重量值。
其余保持不变:
import networkx as nx
import numpy as npy
A = npy.matrix([[2, 2, 7, 0, 0], [0, 2, 6, 3, 0], [0, 0, 0, 2, 1], [0, 0, 0, 0, 4],
[4, 0, 0, 0, 0]])
labels = {0: 'A', 1: 'B', 2: 'C', 3: 'D', 4: 'E'}
G = nx.from_numpy_matrix(A, create_using=nx.DiGraph)
# Set node labels to A, B, C, D, E
nx.set_node_attributes(G, {k: {'label': labels[k]} for k in labels.keys()})
nx.set_edge_attributes(G, {(e[0], e[1]): {'label': e[2]['weight']} for e in G.edges(data=True)})
D = nx.drawing.nx_agraph.to_agraph(G)
# Modify node fillcolor and edge color.
D.node_attr.update(color='blue', style='filled', fillcolor='yellow')
D.edge_attr.update(color='blue', arrowsize=1)
pos = D.layout('dot')
D.draw('Graph.eps')
在这里创建图表是由 G = nx.from_numpy_matrix(A, create_using=nx.DiGraph)
完成的,它将保持权重(与原始实现不同)。
向边缘添加 'label' 属性是通过 nx.set_edge_attributes(G, {(e[0], e[1]): {'label': e[2]['weight']} for e in G.edges(data=True)})
完成的。
结果是: