首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >networkx布局

networkx布局
EN

Stack Overflow用户
提问于 2013-02-25 02:59:35
回答 1查看 783关注 0票数 0

我现在有一个三元组的列表(node1,node2,weight_of_edge)。有没有什么方法可以让中间有边的节点在布局中靠得很近?

EN

回答 1

Stack Overflow用户

发布于 2013-02-26 00:28:02

您应该看看NetworkX库,它提供了许多用于创建和操作网络的工具。

一个基于三元组列表的基本示例:

代码语言:javascript
复制
list_of_triplets = [("n1", "n2", 4),
                    ("n3", "n4", 1),
                    ("n5", "n6", 2),
                    ("n7", "n8", 4),
                    ("n1", "n7", 4),
                    ("n2", "n8", 4),
                    ("n8", "n9", 6),
                    ("n4", "n9", 12),
                    ("n4", "n6", 1),
                    ("n2", "n7", 4),
                    ("n1", "n8", 4)]

# The line below in the code change the list in a format that take
# a weight argument in a dictionary, to be computed by NetworkX

formatted_list = [(node[0], node[1], {"weight":node[2]}) for node in list_of_triplets]

绘制图表的步骤:

代码语言:javascript
复制
import matplotlib.pyplot as plt
import networkx as nx

G = nx.Graph()
G.add_edges_from(formatted_list)
nx.draw(G)
plt.show()

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/15055379

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档