且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何使用PyGraphviz在无向图的边缘添加和显示权重?

更新时间:2023-12-02 12:19:16

您可以在创建边缘时将属性添加到边缘:

You can add attributes to the edges when you create them:

A.add_edge('Alice', 'Emma', weight=5)

或者您可以稍后通过以下方式设置它们:

or you can set them later with:

edge = A.get_edge('Alice', 'Emma')
edge.attr['weight'] = 5

要将文本信息添加到边缘,请给它们一个label属性:

To add textual information to edges, give them a label attribute instead:

edge = A.get_edge('Alice', 'Emma')
edge.attr['label'] = '5'

所有属性都在内部存储为字符串,但是GraphViz会将它们解释为特定类型.请参阅属性文档.

All attributes are internally stored as strings but GraphViz interprets these as specific types; see the attribute documentation.