Layout networkx布局

Layout networkx布局,layout,matplotlib,networkx,Layout,Matplotlib,Networkx,我目前有一个表单的三元组列表(node1、node2、weight_of_edge)。有没有什么方法可以让我进行绘图,这样在布局中,节点之间有一条边的节点就可以靠得很近?您应该看看这个库,它提供了许多用于创建和操作网络的工具 一个基于三胞胎列表的基本示例: list_of_triplets = [("n1", "n2", 4), ("n3", "n4", 1), ("n5", "n6", 2),

我目前有一个表单的三元组列表(node1、node2、weight_of_edge)。有没有什么方法可以让我进行绘图,这样在布局中,节点之间有一条边的节点就可以靠得很近?

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

一个基于三胞胎列表的基本示例:

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]
要绘制图表,请执行以下操作:

import matplotlib.pyplot as plt
import networkx as nx

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

我不知道matplotlib中有这样的功能。有非常强大的网络可视化工具提供了这一点。例如,看一看。