Labeling edges in networkx

后端 未结 2 1478
耶瑟儿~
耶瑟儿~ 2020-12-29 05:26

I´m programming a basic neural network and want to plot it as a picture. For that i created all the nodes and edges i need.

    for l, j in zip(self.layers,         


        
相关标签:
2条回答
  • Here is an example for ploting edge label in networkx, hope it will help you.

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

    0 讨论(0)
  • 2020-12-29 05:40

    you can use the edge attributes of 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()
    
    0 讨论(0)
提交回复
热议问题