从带有边标签的邻接矩阵绘制加权图

Drawing weighted graph from adjacency matrix with edge labels

这是一个加权图的邻接矩阵,元素ai,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()

但是我看不到重量。我对这张照片也不是很满意,它还没有准备好发布。 有谁有好的方法吗?

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, 旋转=True, **kwds)[来源]

绘制边缘标签。

参数:

G (graph) – networkx 图

pos (dictionary) – 以节点为键、位置为值的字典。位置应该是长度为 2 的序列。

ax (Matplotlib Axes object, optional) – 在指定的 Matplotlib 轴上绘制图形。

alpha (float) – 文本透明度(默认=1.0)

edge_labels (dictionary) – 字典中的边标签由边 two-tupleof 文本标签键入(默认=None)。仅绘制字典中键的标签。 label_pos (float) – 边缘标签沿边缘的位置(0=head, 0.5=center, 1=tail)

font_size (int) – 文本标签的字体大小(默认=12)

font_color (string) – 字体颜色字符串(默认='k' black)

font_weight(字符串)– 字体粗细(默认='normal')

font_family(字符串)– 字体系列(默认='sans-serif')

bbox (Matplotlib bbox) – 指定文本框的形状和颜色。

clip_on (bool) – 在轴边界开启剪裁(默认=真)

Return秒:

在边缘键入标签的字典

Return 类型:

词典

您需要指定要绘制边缘标签。为此,您必须致电 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 force-directed 算法,即 non-deterministic,因此您的图表几乎肯定看起来不一样。然而,通常它会产生漂亮的结果,所以这应该不是主要问题。

文档:networkx.drawing.layout.spring_layout,遗憾的是没有提到它是 non-deterministic。

更新:

让标签只是权重(而不是字典):

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()