【问题标题】:Labeling edges in networkx在networkx中标记边缘
【发布时间】:2018-04-16 03:17:28
【问题描述】:

我正在编写一个基本的神经网络,并希望将其绘制成一张图片。为此,我创建了我需要的所有节点和边。

    for l, j in zip(self.layers, range(len(self.layers))):
        for n, i in zip(l.neurons, range(len(l.neurons))):
            fixed_positions[n.identifier] = (j, i)
    for l in self.layers:
        for n in l.neurons:
            for c, w in zip(n.inconnections, n.inconnectionweights):
               g.add_edge(n.identifier, c.identifier)
    fixed_nodes = fixed_positions.keys()
    pos = nx.spring_layout(g, pos=fixed_positions, fixed=fixed_nodes)

蓝色点(想象它们在所有边缘)是我想在边缘添加标签的地方,但我不知道该怎么做。它应该适用于任何合理的净尺寸,即它也应该适用于相应层中的 4、3 和 2 个神经元。

【问题讨论】:

  • 感谢您的回答,不幸的是不是真的,因为我需要修复边缘开始处的标签,否则交叉部分中有一堆乱七八糟的数字,没有人可以阅读它。
  • networkx.github.io/documentation/latest/reference/generated/… 有一个 label_pos 参数,用于确定标签沿边缘的距离(作为 0 和 1 之间的浮点数,0 位于起始节点的末尾,1 位于在另一端)- 这有帮助吗?
  • 我还没有尝试过,但它看起来像我正在寻找的东西,谢谢!
  • @bouteillebleu 这正是我想要的,非常感谢

标签: python networkx


【解决方案1】:

这是一个在networkx中绘制边缘标签的例子,希望对你有所帮助。

import matplotlib.pyplot as plt
import networkx as nx

edges = [['A', 'B'], ['B', 'C'], ['B', 'D']]
G = nx.Graph()
G.add_edges_from(edges)
pos = nx.spring_layout(G)
plt.figure()
nx.draw(
    G, pos, edge_color='black', width=1, linewidths=1,
    node_size=500, node_color='pink', alpha=0.9,
    labels={node: node for node in G.nodes()}
)
nx.draw_networkx_edge_labels(
    G, pos,
    edge_labels={('A', 'B'): 'AB', 
                 ('B', 'C'): 'BC', 
                 ('B', 'D'): 'BD'},
    font_color='red'
)
plt.axis('off')
plt.show()

【讨论】:

  • 非常好!只有,必须添加:import matplotlib.pyplot as plt
【解决方案2】:

您可以使用draw_networkx_edge_labels(edge_labels) 在边缘之​​间绘制标签。

  • 如果没有给出edge_labels,则使用edge的属性。
  • edge_labels 应该是由文本标签的边缘二元组键控的字典。仅绘制字典中键的标签。

要遍历图形的边缘,您可以使用G.edges

  • G.edges 返回(node1, node2) 的列表,其中node1node2 是边的两个节点。
  • G.edges(data=True) 返回(node1, node2, ddict) 的列表,其中ddict 是边缘属性字典。
  • G.edges(data=attr) 返回(node1, node2, ddict[attr]) 的列表
import matplotlib.pyplot as plt
import networkx as nx

G = nx.DiGraph()

G.add_edges_from([(1, 2), (1, 3), (2, 3)])

pos = nx.spring_layout(G)

nx.draw_networkx(G, pos)

edge_labels = dict([((n1, n2), f'{n1}->{n2}')
                    for n1, n2 in G.edges])

nx.draw_networkx_edge_labels(G, pos, edge_labels=edge_labels)

plt.show()

G.edges(data=True)

import matplotlib.pyplot as plt
import networkx as nx

G = nx.Graph()
G.add_edge(1, 2, weight=3)
G.add_edge(2, 3, weight=5)

pos = nx.spring_layout(G)

nx.draw(G, pos, with_labels=True)

edge_labels = dict([((n1, n2), d['weight'])
                    for n1, n2, d in G.edges(data=True)])

nx.draw_networkx_edge_labels(G, pos, edge_labels=edge_labels, label_pos=0.9,
                             font_color='red', font_size=16, font_weight='bold')

plt.show()

【讨论】:

    【解决方案3】:

    可以使用G的边缘属性

    nx.draw(G, with_labels=True, node_color='skyblue', edge_cmap=plt.cm.Blues, pos = pos)
    edge_labels = nx.get_edge_attributes(G,'edge') # key is edge, pls check for your case
    formatted_edge_labels = {(elem[0],elem[1]):edge_labels[elem] for elem in edge_labels} # use this to modify the tuple keyed dict if it has > 2 elements, else ignore
    nx.draw_networkx_edge_labels(G,pos,edge_labels=formatted_edge_labels,font_color='red')
    plt.show()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-02-25
      • 2022-10-24
      • 2020-06-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-12-30
      相关资源
      最近更新 更多