Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/305.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 在networkx中标记边_Python_Networkx - Fatal编程技术网

Python 在networkx中标记边

Python 在networkx中标记边,python,networkx,Python,Networkx,我正在编程一个基本的神经网络,想把它画成一幅图。为此,我创建了所有需要的节点和边 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

我正在编程一个基本的神经网络,想把它画成一幅图。为此,我创建了所有需要的节点和边

    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中绘制边缘标签的示例,希望对您有所帮助

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

您可以使用
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()
可以使用在边之间绘制标签

  • 如果未给出
    edge\u标签
    ,则使用edge的属性
  • edge\u标签
    应该是由edge两个文本标签元组键入的字典。仅绘制字典中键的标签
要遍历图的边,可以使用

  • G.edges
    返回
    (node1,node2)
    的列表,其中
    node1
    node2
    是边的两个节点
  • G.edges(data=True)
    返回
    (node1,node2,ddict)
    的列表,其中
    ddict
    是边属性dict
  • G.edges(data=attr)
    返回
    (node1、node2、ddict[attr])的列表。
导入matplotlib.pyplot作为plt
将networkx导入为nx
G=nx.DiGraph()
G.添加来自([(1,2)、(1,3)、(2,3)]的边
位置=nx.弹簧布置图(G)
nx.绘制网络X(G,位置)
边标签=dict([((n1,n2),f'{n1}->{n2}'))
对于n1,n2,单位为G.边])
nx.绘制网络X边缘标签(G、pos、边缘标签=边缘标签)
plt.show()

G.edges(数据=True)

导入matplotlib.pyplot作为plt
将networkx导入为nx
G=nx.Graph()
G.增加边缘(1,2,重量=3)
G.增加边缘(2,3,重量=5)
位置=nx.弹簧布置图(G)
nx.绘图(G,位置,带_标签=真)
边缘标签=dict([(n1,n2),d[‘重量’)
对于n1、n2、d的G边(数据=真)])
nx.绘制网络x边缘标签(G,位置,边缘标签=边缘标签,标签位置=0.9,
font\u color='red',font\u size=16,font\u weight='bold')
plt.show()

这有帮助吗?谢谢你的回答,不幸的是不是真的,因为我需要将标签固定在边缘的开始处,否则在指令中有一大堆数字,没有人能读得到。有一个
label\u pos
参数,用于确定标签沿边缘的距离(作为介于0和1之间的浮点,0位于起始节点的末端,1位于另一端)-这有帮助吗?我还没有尝试过,但看起来像是我正在搜索的,谢谢@这正是我要找的,谢谢,非常好!仅限于,必须添加:导入matplotlib.pyplot作为plt