I use Networkx in Python to generate the minimum spanning tree of the correlation in my data, and I want to find the (x,y) coordinates of the nodes. I read this question : Exporting Layout Positions for a Graph Using NetworkX and tried to apply it, but I don't really understand why I don't get the expected result.
Here is the code I use :
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()
But instead of having the coordinates I get this :
{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>}}
Do you know how I can change the code to have the coordinates ?
Thank you.