【问题标题】:Python Networkx how to draw edge weight without the word "weight"Python Networkx如何在没有“权重”一词的情况下绘制边缘权重
【发布时间】:2020-04-25 08:15:54
【问题描述】:

from graphviz import *
import networkx as nx
import matplotlib.pyplot as plt

G = nx.DiGraph()
G.add_node(8)
G.add_node(4)
G.add_node(32)
G.add_node(34)
G.add_edge(8,32,weight=0)
G.add_edge(8,34,weight=1)
G.add_edge(4,8,weight=0)
G.add_edge(4,32,weight=1)


pos=graphviz_layout(G)
nx.draw(G, pos)
edge_labels = nx.get_edge_attributes(G,'weight')
nx.draw_networkx_edge_labels(G, pos, labels = edge_labels)
plt.savefig('this.png')
plt.show()

所以我刚刚开始使用 Networkx,所以我不太了解语法。在这种情况下,我怎样才能只画出重量?我在其他一些地方看到相同的代码仅绘制重量而不是“重量”一词。

谢谢。

【问题讨论】:

  • nx.draw_networkx_edge_labels(G,pos,edge_labels={('node1','node2'):'weight-of-1-to-2', ('node2','node4') :'weight-of-2-to-4',font_color='blue') .. 如果需要,请尝试使用标签
  • 看看这个问题:stackoverflow.com/questions/31575634/… 答案中的情节似乎是你想要的。也许代码可以帮助您弄清楚您需要调整什么。
  • @wychmaster 该帖子中的答案与我的代码相同:\

标签: python matplotlib networkx graphviz


【解决方案1】:

nx.draw_networkx_edge_labels()中应该是edge_labels

pos=graphviz_layout(G)
nx.draw(G, pos)
edgeLabels = nx.get_edge_attributes(G,'weight')
nx.draw_networkx_edge_labels(G, pos, edge_labels = edgeLabels)
plt.savefig('this.png')
plt.show()

【讨论】:

    猜你喜欢
    • 2015-04-06
    • 1970-01-01
    • 2014-05-22
    • 1970-01-01
    • 2017-09-24
    • 2013-07-12
    • 1970-01-01
    • 2018-08-14
    • 1970-01-01
    相关资源
    最近更新 更多