我在Python中使用Networkx来生成数据中相关性的最小生成树,我想找到节点的(x,y)坐标。我读了这个问题:Exporting Layout Positions for a Graph Using NetworkX并尝试应用它,但我不明白为什么我没有得到预期的结果。
以下是我使用的代码:
import networkx as nx
import random as rn
import pylab as pl
def create_tree(data):
corr_matrix = define_correlation_matrix(data)
G = nx.Graph(corr_matrix)
pos = nx.minimum_spanning_tree(G)
rn.seed = 5
colors = 'bcgmry'
components = nx.connected_components(pos)
for i in components:
component = pos.subgraph(i)
nx.draw_graphviz(component,
node_color = colors[rn.randint(0, len(colors)-1)],
node_size = 15,
edge_color = [corr_matrix[i][j]*0.5 for (i,j) in component.edges()],
with_labels = True,
labels = dict([(x,data[x]) for x in component.nodes()]))
nx.set_node_attributes(G,'pos',pos)
print G.node
pl.show()
但是我没有得到这个坐标:
{0: {'pos': <networkx.classes.graph.Graph object at 0x10513ea50>}, 1: {'pos': <networkx.classes.graph.Graph object at 0x10513ea50>}, 2: {'pos': <networkx.classes.graph.Graph object at 0x10513ea50>}, 3: {'pos': <networkx.classes.graph.Graph object at 0x10513ea50>}, 4: {'pos': <networkx.classes.graph.Graph object at 0x10513ea50>}, 5: {'pos': <networkx.classes.graph.Graph object at 0x10513ea50>}}
您知道如何更改代码以获得坐标吗?
谢谢。