![](/img/trans.png)
[英]Create a weighted graph from an adjacency matrix in graph-tool, python interface
[英]Drawing weighted graph from adjacency matrix with edge labels
这是加权图的邻接矩阵,其中元素 a i,j是从节点 i 到节点 j 的有向边的权重。
A = [
[0, 1, 0, .8, 0],
[0, 0, .4, 0, .3],
[0, 0, 0, 0, 0],
[0, 0, .6, 0, .7],
[0, 0, 0, .2, 0]]
我的主要目标是生成该图的插图。
我可以像这样在 networkx 中生成一个图形:
import matplotlib.pyplot as plt
import networkx as nx
import numpy as np
G = nx.from_numpy_matrix(np.matrix(A), create_using=nx.DiGraph)
nx.draw(G)
plt.show()
但是我看不到重量。 我对这张图片也不是很满意,它还没有准备好出版。 有没有人有这样做的好方法?
您需要指定要绘制边缘标签。 为此,您必须致电networkx.drawing.nx_pylab.draw_networkx_edge_labels 。
它有一个参数pos
,一个以节点为键、以位置为值的字典。 对节点和标签使用相同的布局很重要,否则它们将不会对齐!
一个简单的方法是让 networkx 处理布局,例如使用spring_layout
。
import matplotlib.pyplot as plt
import networkx as nx
import numpy as np
A = [
[0, 1, 0, .8, 0],
[0, 0, .4, 0, .3],
[0, 0, 0, 0, 0],
[0, 0, .6, 0, .7],
[0, 0, 0, .2, 0]]
G = nx.from_numpy_matrix(np.matrix(A), create_using=nx.DiGraph)
layout = nx.spring_layout(G)
nx.draw(G, layout)
nx.draw_networkx_edge_labels(G, pos=layout)
plt.show()
例子:
请注意, spring_layout
使用非确定性的 Fruchterman-Reingold 力导向算法,因此您的图形几乎肯定看起来不一样。 然而,通常它会产生好看的结果,所以这不应该是一个主要问题。
文档: networkx.drawing.layout.spring_layout ,不幸的是没有提到它是非确定性的。
更新:
让标签只是权重(而不是字典):
labels = nx.get_edge_attributes(G, "weight")
nx.draw_networkx_edge_labels(G, pos=layout, edge_labels=labels)
import matplotlib.pyplot as plt
import networkx as nx
import numpy as np
A = [
[0, 1, 0, .8, 0],
[0, 0, .4, 0, .3],
[0, 0, 0, 0, 0],
[0, 0, .6, 0, .7],
[0, 0, 0, .2, 0]]
G = nx.from_numpy_matrix(np.matrix(A), create_using=nx.DiGraph)
layout = nx.spring_layout(G)
nx.draw(G, layout, node_size=1000, with_labels=True, font_weight='bold', font_size=15)
labels = nx.get_edge_attributes(G,'weight')
nx.draw_networkx_edge_labels(G,pos=layout,edge_labels=labels)
plt.show()
A = [
[0, 1, 0, .8, 0],
[0, 0, .4, 0, .3],
[0, 0, 0, 0, 0],
[0, 0, .6, 0, .7],
[0, 0, 0, .2, 0]]
import matplotlib.pyplot as plt
import networkx as nx
import numpy as np
G = nx.from_numpy_matrix(np.matrix(A))
edge_labels=nx.draw_networkx_edge_labels(G,pos=nx.spring_layout(G))
nx.draw(G)
plt.show()
draw_networkx_edge_labels
draw_networkx_edge_labels(G, pos, edge_labels=None, label_pos=0.5, font_size=10, font_color='k', font_family='sans-serif', font_weight='normal', alpha=1.0, bbox=None, ax=None,旋转=真,**kwds)[来源]
绘制边缘标签。
参数:
G (graph) – 一个 networkx 图
pos (dictionary) – 一个以节点为键、以位置为值的字典。 位置应该是长度为 2 的序列。
ax(Matplotlib Axes 对象,可选)– 在指定的 Matplotlib 轴中绘制图形。
alpha (float) – 文本透明度(默认值=1.0)
edge_labels (dictionary) – 由边二元组文本标签键控的字典中的边标签(默认值=无)。 只绘制字典中键的标签。 label_pos (float) – 边缘标签沿边缘的位置(0=head, 0.5=center, 1=tail)
font_size (int) – 文本标签的字体大小(默认值 = 12)
font_color (string) – 字体颜色字符串 (default='k' black)
font_weight (string) – 字体粗细(默认 = 'normal')
font_family (string) – 字体系列 (default='sans-serif')
bbox (Matplotlib bbox) – 指定文本框的形状和颜色。
clip_on (bool) – 在轴边界处打开裁剪(默认 = True)
返回:
键入在边缘的标签字典
返回类型:
字典
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.