简体   繁体   English

NetworkX图:使用有序列表创建节点

[英]NetworkX graph: creating nodes with ordered list

I am completely new to graphs. 我对图完全陌生。 I have a 213 X 213 distance matrix. 我有一个213 X 213距离矩阵。 I have been trying to visualize the distance matrix using network and my idea is that far apart nodes will appear as separate clusters when the graph will be plotted. 我一直在尝试使用网络可视化距离矩阵,我的想法是,在绘制图形时,相距较远的节点将显示为单独的群集。 So I am creating a graph with nodes representing column index. 所以我正在创建一个带有代表列索引的节点的图形。 I need to keep track of nodes in order to label it afterwards. 我需要跟踪节点以便以后对其进行标记。 I need to add edges in certain order so I need to keep track of nodes and their labels. 我需要按一定顺序添加边,因此需要跟踪节点及其标签。

Here is the code: 这是代码:

import networkx as nx
G = nx.Graph()
G.add_nodes_from(time_pres) ##time_pres is the list of labels that I want specific node to have

 for i in range(212):
    for j in range(i+1, 212):
        color = ['green' if j == i+1 else 'red'][0]
        edges.append((i,j, dist[i,j], 'green')) ##This thing requires allocation of distance as per the order in dist matrirx
        G.add_edge(i,j, dist = dist[i,j], color = 'green')

The way I am doing rite now, it is allocating nodes with id as a number which is not as per the index of labels in time_pres. 我现在进行仪式的方式是,将id分配给节点的数字不是time_pres中标签的索引。

Grateful for your help. 感谢您的帮助。 Thanks!! 谢谢!!

I can answer the question you seem to be asking, but this won't be the end of your troubles. 我可以回答您似乎要问的问题,但这并不能解决您的麻烦。 Specifically, I'll show you where you go wrong. 具体来说,我将告诉您您出了什么问题。

So, we assume that the variable time_pres is defined as follows 因此,我们假设变量time_pres定义如下

time_pres = [('person1', '1878'), ('person2', '1879'), etc)]

Then, 然后,

G.add_nodes_from(time_pres)

Creates the nodes with labels ('person1', '1878') , ('person2', '1879') , etc. These nodes are held in a dictionary, with keys the label of the nodes and values any additional attributes related to each node. 创建带有标签('person1', '1878')('person2', '1879')等的节点。这些节点保存在字典中,并使用键来标记节点的标签并为与每个节点相关的任何其他属性取值节点。 In your case, you have no attributes. 就您而言,您没有属性。 You can also see this from the manual online , or if you type help(G.add_nodes_from) . 您也可以从联机手册中看到此内容,或者键入help(G.add_nodes_from)

You can even see the label of the nodes by typing either of the following lines. 您甚至可以通过键入以下任一行来查看节点的标签。

G.nodes()        # either this
G.node.keys()    # or this

This will print a list of the labels, but since they come from a dictionary, they may not be in the same order as time_pres . 这将打印标签列表,但是由于它们来自字典,因此它们的顺序可能与time_pres You can refer to the nodes by their labels. 您可以通过节点的标签来引用它们。 They don't have any additional id numbers, or anything else. 他们没有其他ID号或其他任何编号。

Now, for adding an edge. 现在,添加一条边缘。 The manual says that any of the two nodes will be added if they are not already in the graph. 手册指出,如果两个节点中的任何一个不在图中,则将添加它们。 So, when you do 所以,当你这样做

G.add_edge(i, j, dist = dist[i,j], color = 'green')

where, i and j are numbers, they are added in the graph since they don't already exist in the graph labels. 其中, ij是数字,因为它们在图形标签中尚不存在,所以将它们添加到图形中。 So, you end up adding the nodes i and j and the edge between them. 因此,最终要添加节点ij以及它们之间的边缘。 Instead, you want to do 相反,您想做

G.add_edge(time_pres[i], time_pres[j], dist = dist[i,j], color = 'green')

This will add an edge between the nodes time_pres[i] and time_pres[j] . 这将在节点time_pres[i]time_pres[j]之间添加一条边。 As far as I understand, this is your aim. 据我了解,这是您的目标。


However, you seem to expect that when you draw the graph, the distance between nodes time_pres[i] and time_pres[j] will be decided by the attribute dist=dist[i,j] in G.add_edge() . 但是,您似乎希望在绘制图形时,节点time_pres[i]time_pres[j]之间的距离将由G.add_edge() dist=dist[i,j]属性决定。 In fact, the position of a node is decided by tuple holding the x and y positions of the node. 实际上,节点的位置是由保持该节点的x和y位置的元组决定的。 From the manual for nx.draw() . nx.draw()手册中获取。

pos : dictionary, optional pos :字典,可选

A dictionary with nodes as keys and positions as values. 以节点为键,位置为值的字典。 If not specified a spring layout positioning will be computed. 如果未指定,将计算弹簧布局位置。 See networkx.layout for functions that compute node positions. 有关计算节点位置的功能,请参见networkx.layout。

If you don't define the node positions, they will be generated randomly. 如果您未定义节点位置,则会随机生成它们。 In your case, you would need a dictionary like 在您的情况下,您将需要像这样的字典

pos = {('person1', '1878'): (23, 10),
       ('person2', '1879'): (18, 11),
       etc}

Then, the coordinates between the nodes i and j would result to a distance equal to dist[i,j] . 然后,节点ij之间的坐标将导致等于dist[i,j]的距离。 You would have to figure out these coordinates, but since you haven't made it clear exactly how you derived the matrix dist , I can't say anything about it. 您将必须弄清楚这些坐标,但是由于您尚未确切说明如何得出矩阵dist ,因此我对此一无所知。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM