0

我开始igraph结合使用 python,networkx因为前者已经实现了network community detection.

现在我只是从一个加权的、非对称的邻接矩阵和节点标签字典开始。我创建了一个有向图 G, in networkx,然后将其转换为,并用标记的节点 igraph graph, g绘制了结果。igraph

import numpy as npy
import networkx as nx
import igraph as ig

# Create adjacency matrix, A, and corresponding directed graph in networkx
A=npy.matrix([[4,7,7,0,0],[3,0,6,0,0],[7,6,0,2,1],[0,0,2,0,4],[0,0,1,4,0]])
G = nx.from_numpy_matrix(A, create_using=nx.DiGraph)

# Dictionary of node labels, simply 'A', 'B', ...'E' for this example
labels_dict={0: 'A', 1: 'B', 2: 'C', 3: 'D', 4: 'E'}
labels=list(labels_dict.values())

# Convert networkx graph to igraph
nx.write_graphml(G,'graph.graphml')
g = ig.read('graph.graphml',format="graphml")

# Plot directed graph using igraph with labeled vertices, 
ig.plot(g,vertex_label =  labels)

这将创建所需的图表,但我想知道最有效labelling的方法edges及其相应的权重。

4

1 回答 1

0

这有效:

ig.plot(g,vertex_label =  labels,vertex_label_size = 10, vertex_size = 20 
,edge_label = [edge for edge in g.es['weight']],edge_width = [.25*edge for edge 
in g.es['weight']])

弧线可能难以查看每个边与哪个权重相关联,这可以通过添加选项 edge_curved=0 来解决。

于 2019-09-15T21:00:55.393 回答