简体   繁体   English

使用networkx从距离矩阵生成图形:不一致-Python

[英]Generating graph from distance matrix using networkx: inconsistency - Python

I have the following distance matrix: 我有以下距离矩阵:

delta =
[[ 0.          0.71370845  0.80903791  0.82955157  0.56964983  0.          0.        ]
 [ 0.71370845  0.          0.99583115  1.          0.79563006  0.71370845
   0.71370845]
 [ 0.80903791  0.99583115  0.          0.90029133  0.81180111  0.80903791
   0.80903791]
 [ 0.82955157  1.          0.90029133  0.          0.97468433  0.82955157
   0.82955157]
 [ 0.56964983  0.79563006  0.81180111  0.97468433  0.          0.56964983
   0.56964983]
 [ 0.          0.71370845  0.80903791  0.82955157  0.56964983  0.          0.        ]
 [ 0.          0.71370845  0.80903791  0.82955157  0.56964983  0.          0.        ]]

And I'm trying to use the networkx library to represent it as a graph. 而且我正在尝试使用networkx库将其表示为图形。 This is my code: 这是我的代码:

import networkx as nx

G = nx.from_numpy_matrix(delta) 
pos = nx.random_layout(G) 

plt.figure(figsize=(7, 7))
for k, p in pos.iteritems():
    plt.scatter(p[0], p[1], marker='o', c=colors[k], s=50, edgecolor='None')
lgd = plt.legend(markers, labels, numpoints=1, bbox_to_anchor=(1.17, 0.5))
plt.tight_layout()
plt.axis('equal')
pt.show()

However, what I see is not what I expect. 但是,我所看到的不是我所期望的。 For instance, consider this output: 例如,考虑以下输出:

在此处输入图片说明

From delta , node 1 is at the same point as node 6 and 7, and far from node 4. I don't see the in the output plot. delta来看,节点1与节点6和7在同一点,并且远离节点4。我在输出图中看不到。 Besides, overtime I run it, it results in another output. 此外,我加班运行它会导致另一个输出。 This is expected, but the distance seem to not be respected. 这是预料之中的,但是距离似乎没有得到尊重。 In the following plot, for example, distances between 1 to 6,7 and 4 changed. 例如,在下图中,1到6.7与4之间的距离发生了变化。

在此处输入图片说明

I can't understand why. 我不明白为什么。

You are using nx.random_layout , which positions the vertices of the graph in random positions drawn from the uniform distribution. 您正在使用nx.random_layout ,它将图形的顶点定位在从均匀分布绘制的随机位置。 There are other layouts, such as the nx.spring_layout , aka nx.fruchterman_reingold_layout , that try to position the vertices such that their distances approximate the given distances. 还有其他布局,例如nx.spring_layout ,又名nx.fruchterman_reingold_layout ,它们试图将顶点定位为使其距离近似于给定距离。

You can use a slightly more consistent layout, maybe shell_layout() or circular_layout(). 您可以使用稍微更一致的布局,例如shell_layout()或Circular_layout()。 Technically, in a generic abstract graph, the depicted location has no real meaning, and each of these functions tends to have a little variance each time you call it as a reflection of that fact. 从技术上讲,在一般的抽象图中,所描绘的位置没有实际含义,并且每次您调用它时,这些功能中的每一个趋向于都有一点变化,以反映该事实。 They simply place nodes in a reasonable fashion according to some predefined pattern. 它们只是根据某种预定义的模式以合理的方式放置节点。

If you want consistent placement you'll have to do it yourself. 如果您想要一致的展示位置,则必须自己完成。

Understand the structure the layout functions produce and use your understanding of the data to produce a more sensible visualization. 了解布局功能产生的结构,并利用对数据的理解来产生更明智的可视化效果。 These functions produce a dictionary keyed on the nodes with values that are lists of length 2. The first entry specifies the node's x location, the second, the y. 这些函数生成一个以长度为2的值列在节点上的字典,第一个条目指定节点的x位置,第二个条目指定y的位置。
Take this example, a graph of network connections between offices. 以这个例子为例,办公室之间的网络连接图。

pos=nx.spring_layout(G)
print pos

might yield something like 可能会产生类似

{'A': [1, 12], 'C': [5, 8], 'B': [4, 11], 'E': [8, 3], 'D': [8, 7], 'F': [6, 1]}

However, I know that since my data represents citites it makes sense to display the nodes at locations that represent their physical locations, so I, instead, build my own dictionary (each node already has an initialized attribute of 'x' and 'y'). 但是,我知道,由于我的数据代表citite,因此在代表其物理位置的位置显示节点是有意义的,所以我改为构建自己的字典(每个节点已经具有“ x”和“ y”的初始化属性)。

pos = {}
for node in G.nodes():
    pos[node] = [G.node[node]["x"], G.node[node]["y"]]

This displays the nodes the same way every time. 每次以相同的方式显示节点。 Create your own pos dictionary in a similar manner based on your matrix. 根据您的矩阵以类似方式创建自己的pos词典。

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

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